Unpooled { public static ByteBuf wrappedBuffer(byte[] array) { if (array.length == 0) { return EMPTY_BUFFER; } return new UnpooledHeapByteBuf(ALLOC, array, array.length); } private  Unpooled(); static ByteBuf buffer(); static ByteBuf directBuffer(); static ByteBuf buffer(int initialCapacity); static ByteBuf directBuffer(int initialCapacity); static ByteBuf buffer(int initialCapacity, int maxCapacity); static ByteBuf directBuffer(int initialCapacity, int maxCapacity); static ByteBuf wrappedBuffer(byte[] array); static ByteBuf wrappedBuffer(byte[] array, int offset, int length); static ByteBuf wrappedBuffer(ByteBuffer buffer); static ByteBuf wrappedBuffer(ByteBuf buffer); static ByteBuf wrappedBuffer(byte[]... arrays); static ByteBuf wrappedBuffer(ByteBuf... buffers); static ByteBuf wrappedBuffer(ByteBuffer... buffers); static ByteBuf wrappedBuffer(int maxNumComponents, byte[]... arrays); static ByteBuf wrappedBuffer(int maxNumComponents, ByteBuf... buffers); static ByteBuf wrappedBuffer(int maxNumComponents, ByteBuffer... buffers); static CompositeByteBuf compositeBuffer(); static CompositeByteBuf compositeBuffer(int maxNumComponents); static ByteBuf copiedBuffer(byte[] array); static ByteBuf copiedBuffer(byte[] array, int offset, int length); static ByteBuf copiedBuffer(ByteBuffer buffer); static ByteBuf copiedBuffer(ByteBuf buffer); static ByteBuf copiedBuffer(byte[]... arrays); static ByteBuf copiedBuffer(ByteBuf... buffers); static ByteBuf copiedBuffer(ByteBuffer... buffers); static ByteBuf copiedBuffer(CharSequence string, Charset charset); static ByteBuf copiedBuffer(
            CharSequence string, int offset, int length, Charset charset); static ByteBuf copiedBuffer(char[] array, Charset charset); static ByteBuf copiedBuffer(char[] array, int offset, int length, Charset charset); static ByteBuf unmodifiableBuffer(ByteBuf buffer); static ByteBuf copyInt(int value); static ByteBuf copyInt(int... values); static ByteBuf copyShort(int value); static ByteBuf copyShort(short... values); static ByteBuf copyShort(int... values); static ByteBuf copyMedium(int value); static ByteBuf copyMedium(int... values); static ByteBuf copyLong(long value); static ByteBuf copyLong(long... values); static ByteBuf copyBoolean(boolean value); static ByteBuf copyBoolean(boolean... values); static ByteBuf copyFloat(float value); static ByteBuf copyFloat(float... values); static ByteBuf copyDouble(double value); static ByteBuf copyDouble(double... values); static ByteBuf unreleasableBuffer(ByteBuf buf); static ByteBuf unmodifiableBuffer(ByteBuf... buffers); static final ByteOrder BIG_ENDIAN; static final ByteOrder LITTLE_ENDIAN; static final ByteBuf EMPTY_BUFFER; }
@Test public void testHashCode() { Map<byte[], Integer> map = new LinkedHashMap<byte[], Integer>(); map.put(EMPTY_BYTES, 1); map.put(new byte[] { 1 }, 32); map.put(new byte[] { 2 }, 33); map.put(new byte[] { 0, 1 }, 962); map.put(new byte[] { 1, 2 }, 994); map.put(new byte[] { 0, 1, 2, 3, 4, 5 }, 63504931); map.put(new byte[] { 6, 7, 8, 9, 0, 1 }, (int) 97180294697L); map.put(new byte[] { -1, -1, -1, (byte) 0xE1 }, 1); for (Entry<byte[], Integer> e: map.entrySet()) { assertEquals( e.getValue().intValue(), ByteBufUtil.hashCode(wrappedBuffer(e.getKey()))); } }
@Test public void testEquals() { ByteBuf a, b; a = wrappedBuffer(new byte[] { 1 }); b = wrappedBuffer(new byte[] { 1, 2 }); assertFalse(ByteBufUtil.equals(a, b)); a = wrappedBuffer(new byte[] { 1, 2, 3 }); b = wrappedBuffer(new byte[] { 1, 2, 3 }); assertTrue(ByteBufUtil.equals(a, b)); a = wrappedBuffer(new byte[] { 1, 2, 3 }); b = wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 1, 3); assertTrue(ByteBufUtil.equals(a, b)); a = wrappedBuffer(new byte[] { 1, 2, 3 }); b = wrappedBuffer(new byte[] { 1, 2, 4 }); assertFalse(ByteBufUtil.equals(a, b)); a = wrappedBuffer(new byte[] { 1, 2, 3 }); b = wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 1, 3); assertFalse(ByteBufUtil.equals(a, b)); a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); b = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); assertTrue(ByteBufUtil.equals(a, b)); a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); b = wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1, 10); assertTrue(ByteBufUtil.equals(a, b)); a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); b = wrappedBuffer(new byte[] { 1, 2, 3, 4, 6, 7, 8, 5, 9, 10 }); assertFalse(ByteBufUtil.equals(a, b)); a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); b = wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 1, 10); assertFalse(ByteBufUtil.equals(a, b)); }
@Test public void testCompare() { List<ByteBuf> expected = new ArrayList<ByteBuf>(); expected.add(wrappedBuffer(new byte[]{1})); expected.add(wrappedBuffer(new byte[]{1, 2})); expected.add(wrappedBuffer(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})); expected.add(wrappedBuffer(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})); expected.add(wrappedBuffer(new byte[]{2})); expected.add(wrappedBuffer(new byte[]{2, 3})); expected.add(wrappedBuffer(new byte[]{2, 3, 4, 5, 6, 7, 8, 9, 10, 11})); expected.add(wrappedBuffer(new byte[]{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13})); expected.add(wrappedBuffer(new byte[]{2, 3, 4}, 1, 1)); expected.add(wrappedBuffer(new byte[]{1, 2, 3, 4}, 2, 2)); expected.add(wrappedBuffer(new byte[]{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, 1, 10)); expected.add(wrappedBuffer(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, 2, 12)); expected.add(wrappedBuffer(new byte[]{2, 3, 4, 5}, 2, 1)); expected.add(wrappedBuffer(new byte[]{1, 2, 3, 4, 5}, 3, 2)); expected.add(wrappedBuffer(new byte[]{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, 2, 10)); expected.add(wrappedBuffer(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 3, 12)); for (int i = 0; i < expected.size(); i ++) { for (int j = 0; j < expected.size(); j ++) { if (i == j) { assertEquals(0, ByteBufUtil.compare(expected.get(i), expected.get(j))); } else if (i < j) { assertTrue(ByteBufUtil.compare(expected.get(i), expected.get(j)) < 0); } else { assertTrue(ByteBufUtil.compare(expected.get(i), expected.get(j)) > 0); } } } }
@Test public void testCompare2() { assertTrue(ByteBufUtil.compare( wrappedBuffer(new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}), wrappedBuffer(new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00})) > 0); assertTrue(ByteBufUtil.compare( wrappedBuffer(new byte[]{(byte) 0xFF}), wrappedBuffer(new byte[]{(byte) 0x00})) > 0); }
@Test public void shouldAllowEmptyBufferToCreateCompositeBuffer() { ByteBuf buf = wrappedBuffer( EMPTY_BUFFER, wrappedBuffer(new byte[16]).order(LITTLE_ENDIAN), EMPTY_BUFFER); try { assertEquals(16, buf.capacity()); } finally { buf.release(); } }
@Test public void testWrappedBuffer() { assertEquals(16, freeLater(wrappedBuffer(ByteBuffer.allocateDirect(16))).capacity()); assertEquals( wrappedBuffer(new byte[] { 1, 2, 3 }), wrappedBuffer(new byte[][] { new byte[] { 1, 2, 3 } })); assertEquals( wrappedBuffer(new byte[] { 1, 2, 3 }), freeLater(wrappedBuffer( new byte[] { 1 }, new byte[] { 2 }, new byte[] { 3 }))); assertEquals( wrappedBuffer(new byte[] { 1, 2, 3 }), wrappedBuffer(new ByteBuf[] { wrappedBuffer(new byte[] { 1, 2, 3 }) })); assertEquals( wrappedBuffer(new byte[] { 1, 2, 3 }), freeLater(wrappedBuffer( wrappedBuffer(new byte[] { 1 }), wrappedBuffer(new byte[] { 2 }), wrappedBuffer(new byte[] { 3 })))); assertEquals( wrappedBuffer(new byte[] { 1, 2, 3 }), wrappedBuffer(new ByteBuffer[] { ByteBuffer.wrap(new byte[] { 1, 2, 3 }) })); assertEquals( wrappedBuffer(new byte[] { 1, 2, 3 }), freeLater(wrappedBuffer( ByteBuffer.wrap(new byte[] { 1 }), ByteBuffer.wrap(new byte[] { 2 }), ByteBuffer.wrap(new byte[] { 3 })))); }
@Test public void testHexDump() { assertEquals("", ByteBufUtil.hexDump(EMPTY_BUFFER)); assertEquals("123456", ByteBufUtil.hexDump(wrappedBuffer( new byte[]{ 0x12, 0x34, 0x56 }))); assertEquals("1234567890abcdef", ByteBufUtil.hexDump(wrappedBuffer( new byte[]{ 0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF }))); }
@Test public void wrappedReadOnlyDirectBuffer() { ByteBuffer buffer = ByteBuffer.allocateDirect(12); for (int i = 0; i < 12; i++) { buffer.put((byte) i); } buffer.flip(); ByteBuf wrapped = wrappedBuffer(buffer.asReadOnlyBuffer()); for (int i = 0; i < 12; i++) { assertEquals((byte) i, wrapped.readByte()); } wrapped.release(); }