BsUtil { static int lexicographicalCompare( final RandomAccessObject data1, final int start1, final int length1, final RandomAccessObject data2, final int start2, final int length2) throws IOException { int bytesLeft = Math.min(length1, length2); data1.seek(start1); data2.seek(start2); while (bytesLeft-- > 0) { final int i1 = data1.readUnsignedByte(); final int i2 = data2.readUnsignedByte(); if (i1 != i2) { return i1 - i2; } } return length1 - length2; }    }
@Test public void lexicographicalCompareTest() throws IOException { String s1 = "this is a string"; String s2 = "that was a string"; byte[] s1b = s1.getBytes(Charset.forName("US-ASCII")); byte[] s2b = s2.getBytes(Charset.forName("US-ASCII")); RandomAccessObject s1ro = new RandomAccessObject.RandomAccessByteArrayObject(s1b); RandomAccessObject s2ro = new RandomAccessObject.RandomAccessByteArrayObject(s2b); int r = BsUtil.lexicographicalCompare(s1ro, 0, s1b.length, s2ro, 0, s2b.length); Assert.assertTrue(r > 0); r = BsUtil.lexicographicalCompare(s1ro, 5, s1b.length - 5, s2ro, 5, s2b.length - 5); Assert.assertTrue(r < 0); r = BsUtil.lexicographicalCompare(s1ro, 7, s1b.length - 7, s2ro, 8, s2b.length - 7); Assert.assertTrue(r < 0); r = BsUtil.lexicographicalCompare(s1ro, 7, s1b.length - 8, s2ro, 8, s2b.length - 8); Assert.assertTrue(r < 0); r = BsUtil.lexicographicalCompare(s1ro, 0, 2, s2ro, 0, 2); Assert.assertEquals(0, r); r = BsUtil.lexicographicalCompare(s1ro, 0, 1, s2ro, 0, 2); Assert.assertTrue(r < 0); r = BsUtil.lexicographicalCompare(s1ro, 0, 2, s2ro, 0, 1); Assert.assertTrue(r > 0); }