Writes a variable-length Long value. @param buf The buffer to write to. @param n The value to write.
(final ChannelBuffer buf, long n)
| 1235 | * @param n The value to write. |
| 1236 | */ |
| 1237 | @SuppressWarnings("fallthrough") |
| 1238 | static void writeVLong(final ChannelBuffer buf, long n) { |
| 1239 | // All those values can be encoded on 1 byte. |
| 1240 | if (n >= -112 && n <= 127) { |
| 1241 | buf.writeByte((byte) n); |
| 1242 | return; |
| 1243 | } |
| 1244 | |
| 1245 | // Set the high bit to indicate that more bytes are to come. |
| 1246 | // Both 0x90 and 0x88 have the high bit set (and are thus negative). |
| 1247 | byte b = (byte) 0x90; // 0b10010000 |
| 1248 | if (n < 0) { |
| 1249 | n = ~n; |
| 1250 | b = (byte) 0x88; // 0b10001000 |
| 1251 | } |
| 1252 | |
| 1253 | { |
| 1254 | long tmp = n; |
| 1255 | do { |
| 1256 | tmp >>>= 8; |
| 1257 | // The first time we decrement `b' here, it's going to move the |
| 1258 | // rightmost `1' in `b' to the right, due to the way 2's complement |
| 1259 | // representation works. So if `n' is positive, and we started with |
| 1260 | // b = 0b10010000, now we'll have b = 0b10001111, which correctly |
| 1261 | // indicates that `n' is positive (5th bit set) and has 1 byte so far |
| 1262 | // (last 3 bits are set). If `n' is negative, and we started with |
| 1263 | // b = 0b10001000, now we'll have b = 0b10000111, which correctly |
| 1264 | // indicates that `n' is negative (5th bit not set) and has 1 byte. |
| 1265 | // Each time we keep decrementing this value, the last remaining 3 |
| 1266 | // bits are going to change according to the format described above. |
| 1267 | b--; |
| 1268 | } while (tmp != 0); |
| 1269 | } |
| 1270 | |
| 1271 | buf.writeByte(b); |
| 1272 | switch (b & 0x07) { // Look at the low 3 bits (the length). |
| 1273 | case 0x00: |
| 1274 | buf.writeLong(n); |
| 1275 | break; |
| 1276 | case 0x01: |
| 1277 | buf.writeInt((int) (n >>> 24)); |
| 1278 | buf.writeMedium((int) n); |
| 1279 | break; |
| 1280 | case 0x02: |
| 1281 | buf.writeMedium((int) (n >>> 24)); |
| 1282 | buf.writeMedium((int) n); |
| 1283 | break; |
| 1284 | case 0x03: |
| 1285 | buf.writeByte((byte) (n >>> 32)); |
| 1286 | case 0x04: |
| 1287 | buf.writeInt((int) n); |
| 1288 | break; |
| 1289 | case 0x05: |
| 1290 | buf.writeMedium((int) n); |
| 1291 | break; |
| 1292 | case 0x06: |
| 1293 | buf.writeShort((short) n); |
| 1294 | break; |
no outgoing calls
no test coverage detected