@Override public int compareTo(byte[] buffer1,int offset1,int length1,byte[] buffer2,int offset2,int length2){
  if (buffer1 == buffer2 && offset1 == offset2 && length1 == length2) {
    return 0;
  }
  final int minLength=Math.min(length1,length2);
  final int minWords=minLength / SIZEOF_LONG;
  final long offset1Adj=offset1 + BYTE_ARRAY_BASE_OFFSET;
  final long offset2Adj=offset2 + BYTE_ARRAY_BASE_OFFSET;
  for (int i=0; i < minWords * SIZEOF_LONG; i+=SIZEOF_LONG) {
    long lw=theUnsafe.getLong(buffer1,offset1Adj + (long)i);
    long rw=theUnsafe.getLong(buffer2,offset2Adj + (long)i);
    long diff=lw ^ rw;
    if (littleEndian) {
      lw=Long.reverseBytes(lw);
      rw=Long.reverseBytes(rw);
    }
    if (diff != 0) {
      return lessThanUnsignedLong(lw,rw) ? -1 : 1;
    }
  }
  int offset=minWords * SIZEOF_LONG;
  if (minLength - offset >= SIZEOF_INT) {
    int il=theUnsafe.getInt(buffer1,offset1Adj + offset);
    int ir=theUnsafe.getInt(buffer2,offset2Adj + offset);
    if (littleEndian) {
      il=Integer.reverseBytes(il);
      ir=Integer.reverseBytes(ir);
    }
    if (il != ir) {
      return lessThanUnsignedInt(il,ir) ? -1 : 1;
    }
    offset+=SIZEOF_INT;
  }
  if (minLength - offset >= SIZEOF_SHORT) {
    short sl=theUnsafe.getShort(buffer1,offset1Adj + offset);
    short sr=theUnsafe.getShort(buffer2,offset2Adj + offset);
    if (littleEndian) {
      sl=Short.reverseBytes(sl);
      sr=Short.reverseBytes(sr);
    }
    if (sl != sr) {
      return lessThanUnsignedShort(sl,sr) ? -1 : 1;
    }
    offset+=SIZEOF_SHORT;
  }
  if (minLength - offset == 1) {
    int a=(buffer1[(int)(offset1 + offset)] & 0xff);
    int b=(buffer2[(int)(offset2 + offset)] & 0xff);
    if (a != b) {
      return a - b;
    }
  }
  return length1 - length2;
}

@ Override public int compareTo ( byte [ ] buffer1 , int offset1 , int length1 , byte [ ] buffer2 , int offset2 , int length2 ) { if ( buffer1 == buffer2 && offset1 == offset2 && length1 == length2 ) { return 0 ; } final int minLength = Math . min ( length1 , length2 ) ; final int minWords = minLength / SIZEOF_LONG ; final long offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET ; final long offset2Adj = offset2 + BYTE_ARRAY_BASE_OFFSET ; for ( int i = 0 ; i < minWords * SIZEOF_LONG ; i += SIZEOF_LONG ) { long lw = theUnsafe . getLong ( buffer1 , offset1Adj + ( long ) i ) ; long rw = theUnsafe . getLong ( buffer2 , offset2Adj + ( long ) i ) ; long diff = lw ^ rw ; if ( littleEndian ) { lw = Long . reverseBytes ( lw ) ; rw = Long . reverseBytes ( rw ) ; } if ( diff != 0 ) { return lessThanUnsignedLong ( lw , rw ) ? - 1 : 1 ; } } int offset = minWords * SIZEOF_LONG ; if ( minLength - offset >= SIZEOF_INT ) { int il = theUnsafe . getInt ( buffer1 , offset1Adj + offset ) ; int ir = theUnsafe . getInt ( buffer2 , offset2Adj + offset ) ; if ( littleEndian ) { il = Integer . reverseBytes ( il ) ; ir = Integer . reverseBytes ( ir ) ; } if ( il != ir ) { return lessThanUnsignedInt ( il , ir ) ? - 1 : 1 ; } offset += SIZEOF_INT ; } if ( minLength - offset >= SIZEOF_SHORT ) { short sl = theUnsafe . getShort ( buffer1 , offset1Adj + offset ) ; short sr = theUnsafe . getShort ( buffer2 , offset2Adj + offset ) ; if ( littleEndian ) { sl = Short . reverseBytes ( sl ) ; sr = Short . reverseBytes ( sr ) ; } if ( sl != sr ) { return lessThanUnsignedShort ( sl , sr ) ? - 1 : 1 ; } offset += SIZEOF_SHORT ; } if ( minLength - offset == 1 ) { int a = ( buffer1 [ ( int ) ( offset1 + offset ) ] & 0xff ) ; int b = ( buffer2 [ ( int ) ( offset2 + offset ) ] & 0xff ) ; if ( a != b ) { return a - b ; } } return length1 - length2 ; }
lexicographically compare two arrays .
