VarInt { public static void putLong(ByteBuffer dest, long val) { if (val < 0) { throw new IllegalArgumentException(String.format("Value (%d) was negative", val)); } for (int shift = 63; shift >= 0; shift -= 7) { long v2 = val >> shift; if (v2 != 0 || shift == 0) { byte b = (byte)((v2 & 0x7f) | (shift == 0 ? 0 : 0x80)); dest.put(b); } } } private  VarInt(); static int getInt(ByteBuffer buffer); static long getLong(ByteBuffer buffer); static void putInt(ByteBuffer dest, int val); static void writeInt(OutputStream out, int val); static void putLong(ByteBuffer dest, long val); static void writeLong(OutputStream out, long val); static int calculateIntLength(int val); static int calculateLongLength(long val);  }
@Test public void longMatchesSpecTest() { ByteBuffer buffer = ByteBuffer.allocate(16); VarInt.putLong(buffer, 0xffffffffL); int size = buffer.position(); buffer.flip(); byte[] data = new byte[size]; buffer.get(data); assertArrayEquals(new byte[]{ (byte) 0x8F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x7F, }, data); }
@Test(expected = IllegalArgumentException.class) public void testNegativeLong1() { ByteBuffer buffer = ByteBuffer.allocate(32); VarInt.putLong(buffer, Integer.MIN_VALUE); }
@Test(expected = IllegalArgumentException.class) public void testNegativeLong2() { ByteBuffer buffer = ByteBuffer.allocate(32); VarInt.putLong(buffer, -1); }