code: public static int compareSubArrays(ByteBuffer bytes1, int offset1, ByteBuffer bytes2, int offset2, int length)
      {
          if (bytes1 == null)
              return bytes2 == null ? 0 : -1;
          if (bytes2 == null) return 1;
  
          assert bytes1.limit() >= offset1 + length : "The first byte array isn't long enough for the specified offset and length.";
          assert bytes2.limit() >= offset2 + length : "The second byte array isn't long enough for the specified offset and length.";
          for (int i = 0; i < length; i++)
          {
              byte byte1 = bytes1.get(offset1 + i);
              byte byte2 = bytes2.get(offset2 + i);
              if (byte1 == byte2)
                  continue;
              // compare non-equal bytes as unsigned
              return (byte1 & 0xFF) < (byte2 & 0xFF) ? -1 : 1;
          }
          return 0;
      }
docs: Compare two ByteBuffer at specified offsets for length.
  Compares the non equal bytes as unsigned.
  @param bytes1 First byte buffer to compare.
  @param offset1 Position to start the comparison at in the first array.
  @param bytes2 Second byte buffer to compare.
  @param offset2 Position to start the comparison at in the second array.
  @param length How many bytes to compare?
  @return -1 if byte1 is less than byte2, 1 if byte2 is less than byte1 or 0 if equal.