ByteBufUtil { public static boolean equals(ByteBuf a, int aStartIndex, ByteBuf b, int bStartIndex, int length) { if (aStartIndex < 0 || bStartIndex < 0 || length < 0) { throw new IllegalArgumentException("All indexes and lengths must be non-negative"); } if (a.writerIndex() - length < aStartIndex || b.writerIndex() - length < bStartIndex) { return false; } final int longCount = length >>> 3; final int byteCount = length & 7; if (a.order() == b.order()) { for (int i = longCount; i > 0; i --) { if (a.getLong(aStartIndex) != b.getLong(bStartIndex)) { return false; } aStartIndex += 8; bStartIndex += 8; } } else { for (int i = longCount; i > 0; i --) { if (a.getLong(aStartIndex) != swapLong(b.getLong(bStartIndex))) { return false; } aStartIndex += 8; bStartIndex += 8; } } for (int i = byteCount; i > 0; i --) { if (a.getByte(aStartIndex) != b.getByte(bStartIndex)) { return false; } aStartIndex ++; bStartIndex ++; } return true; } private  ByteBufUtil(); static String hexDump(ByteBuf buffer); static String hexDump(ByteBuf buffer, int fromIndex, int length); static String hexDump(byte[] array); static String hexDump(byte[] array, int fromIndex, int length); static byte decodeHexByte(CharSequence s, int pos); static byte[] decodeHexDump(CharSequence hexDump); static byte[] decodeHexDump(CharSequence hexDump, int fromIndex, int length); static boolean ensureWritableSuccess(int ensureWritableResult); static int hashCode(ByteBuf buffer); static int indexOf(ByteBuf needle, ByteBuf haystack); static boolean equals(ByteBuf a, int aStartIndex, ByteBuf b, int bStartIndex, int length); static boolean equals(ByteBuf bufferA, ByteBuf bufferB); static int compare(ByteBuf bufferA, ByteBuf bufferB); static int indexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value); static short swapShort(short value); static int swapMedium(int value); static int swapInt(int value); static long swapLong(long value); @SuppressWarnings("deprecation") static ByteBuf writeShortBE(ByteBuf buf, int shortValue); @SuppressWarnings("deprecation") static ByteBuf setShortBE(ByteBuf buf, int index, int shortValue); @SuppressWarnings("deprecation") static ByteBuf writeMediumBE(ByteBuf buf, int mediumValue); static ByteBuf readBytes(ByteBufAllocator alloc, ByteBuf buffer, int length); static ByteBuf writeUtf8(ByteBufAllocator alloc, CharSequence seq); static int writeUtf8(ByteBuf buf, CharSequence seq); static int utf8MaxBytes(CharSequence seq); static ByteBuf writeAscii(ByteBufAllocator alloc, CharSequence seq); static int writeAscii(ByteBuf buf, CharSequence seq); static ByteBuf encodeString(ByteBufAllocator alloc, CharBuffer src, Charset charset); static ByteBuf encodeString(ByteBufAllocator alloc, CharBuffer src, Charset charset, int extraCapacity); static ByteBuf threadLocalDirectBuffer(); static byte[] getBytes(ByteBuf buf); static byte[] getBytes(ByteBuf buf, int start, int length); static byte[] getBytes(ByteBuf buf, int start, int length, boolean copy); static void copy(AsciiString src, ByteBuf dst); static void copy(AsciiString src, int srcIdx, ByteBuf dst, int dstIdx, int length); static void copy(AsciiString src, int srcIdx, ByteBuf dst, int length); static String prettyHexDump(ByteBuf buffer); static String prettyHexDump(ByteBuf buffer, int offset, int length); static void appendPrettyHexDump(StringBuilder dump, ByteBuf buf); static void appendPrettyHexDump(StringBuilder dump, ByteBuf buf, int offset, int length); static boolean isText(ByteBuf buf, Charset charset); static boolean isText(ByteBuf buf, int index, int length, Charset charset);  }
@Test public void equalsBufferSubsections() { byte[] b1 = new byte[128]; byte[] b2 = new byte[256]; Random rand = new Random(); rand.nextBytes(b1); rand.nextBytes(b2); final int iB1 = b1.length / 2; final int iB2 = iB1 + b1.length; final int length = b1.length - iB1; System.arraycopy(b1, iB1, b2, iB2, length); assertTrue(ByteBufUtil.equals(Unpooled.wrappedBuffer(b1), iB1, Unpooled.wrappedBuffer(b2), iB2, length)); }
@Test public void notEqualsBufferSubsections() { byte[] b1 = new byte[50]; byte[] b2 = new byte[256]; Random rand = new Random(); rand.nextBytes(b1); rand.nextBytes(b2); final int iB1 = b1.length / 2; final int iB2 = iB1 + b1.length; final int length = b1.length - iB1; System.arraycopy(b1, iB1, b2, iB2, length); int diffIndex = random(rand, iB1, iB1 + length - 1); ++b1[diffIndex]; assertFalse(ByteBufUtil.equals(Unpooled.wrappedBuffer(b1), iB1, Unpooled.wrappedBuffer(b2), iB2, length)); }
@Test public void notEqualsBufferOverflow() { byte[] b1 = new byte[8]; byte[] b2 = new byte[16]; Random rand = new Random(); rand.nextBytes(b1); rand.nextBytes(b2); final int iB1 = b1.length / 2; final int iB2 = iB1 + b1.length; final int length = b1.length - iB1; System.arraycopy(b1, iB1, b2, iB2, length - 1); assertFalse(ByteBufUtil.equals(Unpooled.wrappedBuffer(b1), iB1, Unpooled.wrappedBuffer(b2), iB2, Math.max(b1.length, b2.length) * 2)); }
@Test (expected = IllegalArgumentException.class) public void notEqualsBufferUnderflow() { byte[] b1 = new byte[8]; byte[] b2 = new byte[16]; Random rand = new Random(); rand.nextBytes(b1); rand.nextBytes(b2); final int iB1 = b1.length / 2; final int iB2 = iB1 + b1.length; final int length = b1.length - iB1; System.arraycopy(b1, iB1, b2, iB2, length - 1); assertFalse(ByteBufUtil.equals(Unpooled.wrappedBuffer(b1), iB1, Unpooled.wrappedBuffer(b2), iB2, -1)); }