ByteComparator implements Comparator<byte[]> { @Override public int compare(byte[] o1, byte[] o2) { if (o1 == o2) { return 0; } if (o1 == null) { return -1; } if (o2 == null) { return 1; } if (o1.length != o2.length) { return o1.length - o2.length; } for (int i = 0; i < o1.length; i++) { int b1 = o1[i]; int b2 = o2[i]; if (b1 != b2) { return b1 - b2; } } return 0; }  @Override int compare(byte[] o1, byte[] o2);  }
@Test public void testCompare() { Comparator<byte[]> comp = new ByteComparator(); byte[] a = new byte[] { 0x00, 0x01, 0x02 }, b = new byte[] { 0x03, 0x04, 0x05 }; byte[] c = new byte[] { 0x00, 0x01, 0x02 }, d = new byte[] { 0x00 }; Assert.assertTrue(0 > comp.compare(a, b)); Assert.assertTrue(0 == comp.compare(a, c)); Assert.assertTrue(0 < comp.compare(b, a)); Assert.assertTrue(0 < comp.compare(a, d)); Assert.assertTrue(0 == comp.compare(a, a)); Assert.assertTrue(0 < comp.compare(a, null)); Assert.assertTrue(0 > comp.compare(null, a)); }