/** * Compares the two given byte sequences, byte by byte, returning a negative, * zero, or positive result if the first sequence is less than, equal to, or * greater than the second. The comparison is performed starting with the * first byte of each sequence, and proceeds until a pair of bytes differs, * or one sequence runs out of byte (is shorter). A shorter sequence is * considered less than a longer one. * * @param bs1 first byte sequence to compare * @param bs2 second byte sequence to compare * @return comparison result */ public static int compareBytes(ByteSequence bs1, ByteSequence bs2) { int minLen = Math.min(bs1.length(), bs2.length()); for (int i = 0; i < minLen; i++) { int a = (bs1.byteAt(i) & 0xff); int b = (bs2.byteAt(i) & 0xff); if (a != b) { return a - b; } } return bs1.length() - bs2.length(); }
/** * Compares the two given byte sequences, byte by byte, returning a negative, * zero, or positive result if the first sequence is less than, equal to, or * greater than the second. The comparison is performed starting with the * first byte of each sequence, and proceeds until a pair of bytes differs, * or one sequence runs out of byte (is shorter). A shorter sequence is * considered less than a longer one. * * @param bs1 first byte sequence to compare * @param bs2 second byte sequence to compare * @return comparison result */ public static int compareBytes(ByteSequence bs1, ByteSequence bs2) { int minLen = Math.min(bs1.length(), bs2.length()); for (int i = 0; i < minLen; i++) { int a = (bs1.byteAt(i) & 0xff); int b = (bs2.byteAt(i) & 0xff); if (a != b) { return a - b; } } return bs1.length() - bs2.length(); }
@Override public int hashCode() { int hash = 1; if (isBackedByArray()) { byte[] data = getBackingArray(); int end = offset() + length(); for (int i = offset(); i < end; i++) hash = (31 * hash) + data[i]; } else { for (int i = 0; i < length(); i++) hash = (31 * hash) + byteAt(i); } return hash; }
@Override public int hashCode() { int hash = 1; if (isBackedByArray()) { byte[] data = getBackingArray(); int end = offset() + length(); for (int i = offset(); i < end; i++) hash = (31 * hash) + data[i]; } else { for (int i = 0; i < length(); i++) hash = (31 * hash) + byteAt(i); } return hash; }