@Test public void testVIntToBytes() { for (int i = -10000; i <= 10000; i++) { byte[] vint = Bytes.vintToBytes(i); int vintSize = vint.length; byte[] vint2 = new byte[vint.length]; assertEquals(vintSize, ByteUtil.vintToBytes(vint2, 0, i)); assertTrue(Bytes.BYTES_COMPARATOR.compare(vint,vint2) == 0); } }
public static int vintToBytes(byte[] result, int offset, final long vint) { long i = vint; if (i >= -112 && i <= 127) { result[offset] = (byte) i; return 1; } int len = -112; if (i < 0) { i ^= -1L; len = -120; } long tmp = i; while (tmp != 0) { tmp = tmp >> 8; len--; } result[offset++] = (byte) len; len = (len < -120) ? -(len + 120) : -(len + 112); for (int idx = len; idx != 0; idx--) { int shiftbits = (idx - 1) * 8; long mask = 0xFFL << shiftbits; result[offset++] = (byte)((i & mask) >> shiftbits); } return len + 1; }
ByteUtil { public static int vintToBytes(byte[] result, int offset, final long vint) { long i = vint; if (i >= -112 && i <= 127) { result[offset] = (byte) i; return 1; } int len = -112; if (i < 0) { i ^= -1L; len = -120; } long tmp = i; while (tmp != 0) { tmp = tmp >> 8; len--; } result[offset++] = (byte) len; len = (len < -120) ? -(len + 120) : -(len + 112); for (int idx = len; idx != 0; idx--) { int shiftbits = (idx - 1) * 8; long mask = 0xFFL << shiftbits; result[offset++] = (byte)((i & mask) >> shiftbits); } return len + 1; } }
ByteUtil { public static int vintToBytes(byte[] result, int offset, final long vint) { long i = vint; if (i >= -112 && i <= 127) { result[offset] = (byte) i; return 1; } int len = -112; if (i < 0) { i ^= -1L; len = -120; } long tmp = i; while (tmp != 0) { tmp = tmp >> 8; len--; } result[offset++] = (byte) len; len = (len < -120) ? -(len + 120) : -(len + 112); for (int idx = len; idx != 0; idx--) { int shiftbits = (idx - 1) * 8; long mask = 0xFFL << shiftbits; result[offset++] = (byte)((i & mask) >> shiftbits); } return len + 1; }  }
ByteUtil { public static int vintToBytes(byte[] result, int offset, final long vint) { long i = vint; if (i >= -112 && i <= 127) { result[offset] = (byte) i; return 1; } int len = -112; if (i < 0) { i ^= -1L; len = -120; } long tmp = i; while (tmp != 0) { tmp = tmp >> 8; len--; } result[offset++] = (byte) len; len = (len < -120) ? -(len + 120) : -(len + 112); for (int idx = len; idx != 0; idx--) { int shiftbits = (idx - 1) * 8; long mask = 0xFFL << shiftbits; result[offset++] = (byte)((i & mask) >> shiftbits); } return len + 1; }  static byte[] toBytes(byte[][] byteArrays); static byte[][] toByteArrays(byte[] b, int length); static byte[][] toByteArrays(byte[] b, int offset, int length); static byte[] serializeVIntArray(int[] intArray); static byte[] serializeVIntArray(int[] intArray, int encodedLength); static void serializeVIntArray(DataOutput output, int[] intArray); static void serializeVIntArray(DataOutput output, int[] intArray, int encodedLength); static long[] readFixedLengthLongArray(DataInput input, int length); static void writeFixedLengthLongArray(DataOutput output, long[] longArray); static int[] deserializeVIntArray(byte[] b); static int[] deserializeVIntArray(DataInput in); static int[] deserializeVIntArray(DataInput in, int length); static int[] deserializeVIntArray(byte[] b, int length); static byte[] concat(byte[] first, byte[]... rest); static T[] concat(T[] first, T[]... rest); static byte[] concat(ColumnModifier columnModifier, ImmutableBytesWritable... writables); static int vintFromBytes(byte[] buffer, int offset); static int vintFromBytes(ImmutableBytesWritable ptr); static long vlongFromBytes(ImmutableBytesWritable ptr); static int vintToBytes(byte[] result, int offset, final long vint); static byte[] nextKey(byte[] key); static boolean nextKey(byte[] key, int length); static boolean nextKey(byte[] key, int offset, int length); static byte[] previousKey(byte[] key); static boolean previousKey(byte[] key, int length); static boolean previousKey(byte[] key, int offset, int length); static byte[] fillKey(byte[] key, int length); static int getSize(CharSequence sequence); static boolean isInclusive(CompareOp op); static boolean compare(CompareOp op, int compareResult); static byte[] copyKeyBytesIfNecessary(ImmutableBytesWritable ptr); static KeyRange getKeyRange(byte[] key, CompareOp op, PDataType type); }
ByteUtil { public static int vintToBytes(byte[] result, int offset, final long vint) { long i = vint; if (i >= -112 && i <= 127) { result[offset] = (byte) i; return 1; } int len = -112; if (i < 0) { i ^= -1L; len = -120; } long tmp = i; while (tmp != 0) { tmp = tmp >> 8; len--; } result[offset++] = (byte) len; len = (len < -120) ? -(len + 120) : -(len + 112); for (int idx = len; idx != 0; idx--) { int shiftbits = (idx - 1) * 8; long mask = 0xFFL << shiftbits; result[offset++] = (byte)((i & mask) >> shiftbits); } return len + 1; }  static byte[] toBytes(byte[][] byteArrays); static byte[][] toByteArrays(byte[] b, int length); static byte[][] toByteArrays(byte[] b, int offset, int length); static byte[] serializeVIntArray(int[] intArray); static byte[] serializeVIntArray(int[] intArray, int encodedLength); static void serializeVIntArray(DataOutput output, int[] intArray); static void serializeVIntArray(DataOutput output, int[] intArray, int encodedLength); static long[] readFixedLengthLongArray(DataInput input, int length); static void writeFixedLengthLongArray(DataOutput output, long[] longArray); static int[] deserializeVIntArray(byte[] b); static int[] deserializeVIntArray(DataInput in); static int[] deserializeVIntArray(DataInput in, int length); static int[] deserializeVIntArray(byte[] b, int length); static byte[] concat(byte[] first, byte[]... rest); static T[] concat(T[] first, T[]... rest); static byte[] concat(ColumnModifier columnModifier, ImmutableBytesWritable... writables); static int vintFromBytes(byte[] buffer, int offset); static int vintFromBytes(ImmutableBytesWritable ptr); static long vlongFromBytes(ImmutableBytesWritable ptr); static int vintToBytes(byte[] result, int offset, final long vint); static byte[] nextKey(byte[] key); static boolean nextKey(byte[] key, int length); static boolean nextKey(byte[] key, int offset, int length); static byte[] previousKey(byte[] key); static boolean previousKey(byte[] key, int length); static boolean previousKey(byte[] key, int offset, int length); static byte[] fillKey(byte[] key, int length); static int getSize(CharSequence sequence); static boolean isInclusive(CompareOp op); static boolean compare(CompareOp op, int compareResult); static byte[] copyKeyBytesIfNecessary(ImmutableBytesWritable ptr); static KeyRange getKeyRange(byte[] key, CompareOp op, PDataType type); static final byte[] EMPTY_BYTE_ARRAY; static final ImmutableBytesPtr EMPTY_BYTE_ARRAY_PTR; static final Comparator<ImmutableBytesPtr> BYTES_PTR_COMPARATOR; }