@Test public void test_compareTo() { byte[] b1 = new byte[] {5, 6, 7}; byte[] b2 = new byte[] {1, 2, 3, 4, 5, 6 ,7, 8, 9}; assertThat(compareTo(b1, 0, b1.length, b2, 4, b1.length), is(0)); }
public static int compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) { if (buffer1 == buffer2 && offset1 == offset2 && length1 == length2) { return 0; } int minLength = Math.min(length1, length2); int minWords = minLength / Longs.BYTES; int offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET; int offset2Adj = offset2 + BYTE_ARRAY_BASE_OFFSET; for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) { long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i); long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i); long diff = lw ^ rw; if (diff != 0) { if (!littleEndian) { return lessThanUnsigned(lw, rw) ? -1 : 1; } int n = 0; int y; int x = (int) diff; if (x == 0) { x = (int) (diff >>> 32); n = 32; } y = x << 16; if (y == 0) { n += 16; } else { x = y; } y = x << 8; if (y == 0) { n += 8; } return (int) (((lw >>> n) & 0xFFL) - ((rw >>> n) & 0xFFL)); } } for (int i = minWords * Longs.BYTES; i < minLength; i++) { int result = UnsignedBytes.compare( buffer1[offset1 + i], buffer2[offset2 + i]); if (result != 0) { return result; } } return length1 - length2; }
BytesUtils { public static int compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) { if (buffer1 == buffer2 && offset1 == offset2 && length1 == length2) { return 0; } int minLength = Math.min(length1, length2); int minWords = minLength / Longs.BYTES; int offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET; int offset2Adj = offset2 + BYTE_ARRAY_BASE_OFFSET; for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) { long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i); long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i); long diff = lw ^ rw; if (diff != 0) { if (!littleEndian) { return lessThanUnsigned(lw, rw) ? -1 : 1; } int n = 0; int y; int x = (int) diff; if (x == 0) { x = (int) (diff >>> 32); n = 32; } y = x << 16; if (y == 0) { n += 16; } else { x = y; } y = x << 8; if (y == 0) { n += 8; } return (int) (((lw >>> n) & 0xFFL) - ((rw >>> n) & 0xFFL)); } } for (int i = minWords * Longs.BYTES; i < minLength; i++) { int result = UnsignedBytes.compare( buffer1[offset1 + i], buffer2[offset2 + i]); if (result != 0) { return result; } } return length1 - length2; } }
BytesUtils { public static int compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) { if (buffer1 == buffer2 && offset1 == offset2 && length1 == length2) { return 0; } int minLength = Math.min(length1, length2); int minWords = minLength / Longs.BYTES; int offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET; int offset2Adj = offset2 + BYTE_ARRAY_BASE_OFFSET; for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) { long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i); long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i); long diff = lw ^ rw; if (diff != 0) { if (!littleEndian) { return lessThanUnsigned(lw, rw) ? -1 : 1; } int n = 0; int y; int x = (int) diff; if (x == 0) { x = (int) (diff >>> 32); n = 32; } y = x << 16; if (y == 0) { n += 16; } else { x = y; } y = x << 8; if (y == 0) { n += 8; } return (int) (((lw >>> n) & 0xFFL) - ((rw >>> n) & 0xFFL)); } } for (int i = minWords * Longs.BYTES; i < minLength; i++) { int result = UnsignedBytes.compare( buffer1[offset1 + i], buffer2[offset2 + i]); if (result != 0) { return result; } } return length1 - length2; }  }
BytesUtils { public static int compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) { if (buffer1 == buffer2 && offset1 == offset2 && length1 == length2) { return 0; } int minLength = Math.min(length1, length2); int minWords = minLength / Longs.BYTES; int offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET; int offset2Adj = offset2 + BYTE_ARRAY_BASE_OFFSET; for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) { long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i); long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i); long diff = lw ^ rw; if (diff != 0) { if (!littleEndian) { return lessThanUnsigned(lw, rw) ? -1 : 1; } int n = 0; int y; int x = (int) diff; if (x == 0) { x = (int) (diff >>> 32); n = 32; } y = x << 16; if (y == 0) { n += 16; } else { x = y; } y = x << 8; if (y == 0) { n += 8; } return (int) (((lw >>> n) & 0xFFL) - ((rw >>> n) & 0xFFL)); } } for (int i = minWords * Longs.BYTES; i < minLength; i++) { int result = UnsignedBytes.compare( buffer1[offset1 + i], buffer2[offset2 + i]); if (result != 0) { return result; } } return length1 - length2; }  static byte[] add(byte[] arr, int n); static byte[] remove(byte[] arr, int n); static int binarySearch(byte[] a, int key); static void write(DataOutput out, Object value); static Object read(DataInput in); static byte toByte(boolean value); static byte[] toBytes(long[] values); static byte[] toBytes(int[] values); static byte[] toBytes(short[] values); static byte[] toBytes(byte[] values); static byte[] toBytes(boolean[] values); static byte[] toBytes(float[] values); static byte[] toBytes(double[] values); static byte[] toBytes(String[] strings); static byte[] toBytes(Reference reference); static byte[] toBytes(ReferenceList reference); static Reference toReference(byte[] value, int offset); static ReferenceList toReferences(byte[] value, int offset); static ReferenceList toReferences(byte[] value, int offset, int length); static boolean[] toBooleans(byte[] value, int offset, int num); static boolean[] toBooleans(byte[] value, int offset); static List<Boolean> toBooleanList(byte[] value, int offset); static long[] toLongs(byte[] value, int offset, int num); static long[] toLongs(byte[] value, int offset); static List<Long> toLongList(byte[] value, int offset); static int[] toInts(byte[] value, int offset, int num); static int[] toInts(byte[] value, int offset); static List<Integer> toIntList(byte[] value, int offset); static short[] toShorts(byte[] value, int offset, int num); static short[] toShorts(byte[] value, int offset); static List<Short> toShortList(byte[] value, int offset); static byte[] toBytes(byte[] value, int offset, int num); static byte[] toBytes(byte[] value, int offset); static List<Byte> toByteList(byte[] value, int offset); static double[] toDoubles(byte[] value, int offset, int num); static double[] toDoubles(byte[] value, int offset); static List<Double> toDoubleList(byte[] value, int offset); static float[] toFloats(byte[] value, int offset, int num); static float[] toFloats(byte[] value, int offset); static List<Float> toFloatList(byte[] value, int offset); static String[] toStrings(byte[] value, int offset, int length); static String[] toStrings(byte[] value, int offset); static List<String> toStringList(byte[] value, int offset); static short getShort(final byte[] b, final int offset); static float getFloat(final byte[] b, final int offset); static double getDouble(final byte[] b, final int offset); static String getString(final byte[] b, final int offset); static ByteBuffer allocate(int length); static int compareTo(byte[] buffer1, int offset1, int length1,
                         byte[] buffer2, int offset2, int length2); }
BytesUtils { public static int compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) { if (buffer1 == buffer2 && offset1 == offset2 && length1 == length2) { return 0; } int minLength = Math.min(length1, length2); int minWords = minLength / Longs.BYTES; int offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET; int offset2Adj = offset2 + BYTE_ARRAY_BASE_OFFSET; for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) { long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i); long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i); long diff = lw ^ rw; if (diff != 0) { if (!littleEndian) { return lessThanUnsigned(lw, rw) ? -1 : 1; } int n = 0; int y; int x = (int) diff; if (x == 0) { x = (int) (diff >>> 32); n = 32; } y = x << 16; if (y == 0) { n += 16; } else { x = y; } y = x << 8; if (y == 0) { n += 8; } return (int) (((lw >>> n) & 0xFFL) - ((rw >>> n) & 0xFFL)); } } for (int i = minWords * Longs.BYTES; i < minLength; i++) { int result = UnsignedBytes.compare( buffer1[offset1 + i], buffer2[offset2 + i]); if (result != 0) { return result; } } return length1 - length2; }  static byte[] add(byte[] arr, int n); static byte[] remove(byte[] arr, int n); static int binarySearch(byte[] a, int key); static void write(DataOutput out, Object value); static Object read(DataInput in); static byte toByte(boolean value); static byte[] toBytes(long[] values); static byte[] toBytes(int[] values); static byte[] toBytes(short[] values); static byte[] toBytes(byte[] values); static byte[] toBytes(boolean[] values); static byte[] toBytes(float[] values); static byte[] toBytes(double[] values); static byte[] toBytes(String[] strings); static byte[] toBytes(Reference reference); static byte[] toBytes(ReferenceList reference); static Reference toReference(byte[] value, int offset); static ReferenceList toReferences(byte[] value, int offset); static ReferenceList toReferences(byte[] value, int offset, int length); static boolean[] toBooleans(byte[] value, int offset, int num); static boolean[] toBooleans(byte[] value, int offset); static List<Boolean> toBooleanList(byte[] value, int offset); static long[] toLongs(byte[] value, int offset, int num); static long[] toLongs(byte[] value, int offset); static List<Long> toLongList(byte[] value, int offset); static int[] toInts(byte[] value, int offset, int num); static int[] toInts(byte[] value, int offset); static List<Integer> toIntList(byte[] value, int offset); static short[] toShorts(byte[] value, int offset, int num); static short[] toShorts(byte[] value, int offset); static List<Short> toShortList(byte[] value, int offset); static byte[] toBytes(byte[] value, int offset, int num); static byte[] toBytes(byte[] value, int offset); static List<Byte> toByteList(byte[] value, int offset); static double[] toDoubles(byte[] value, int offset, int num); static double[] toDoubles(byte[] value, int offset); static List<Double> toDoubleList(byte[] value, int offset); static float[] toFloats(byte[] value, int offset, int num); static float[] toFloats(byte[] value, int offset); static List<Float> toFloatList(byte[] value, int offset); static String[] toStrings(byte[] value, int offset, int length); static String[] toStrings(byte[] value, int offset); static List<String> toStringList(byte[] value, int offset); static short getShort(final byte[] b, final int offset); static float getFloat(final byte[] b, final int offset); static double getDouble(final byte[] b, final int offset); static String getString(final byte[] b, final int offset); static ByteBuffer allocate(int length); static int compareTo(byte[] buffer1, int offset1, int length1,
                         byte[] buffer2, int offset2, int length2);  }