From 8d63aae484a4a57e20f0d8f659118efabccac492 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 7 Apr 2016 09:56:03 +0200 Subject: [PATCH] ByteBuf.readBytes(...) should use the allocator of the buffer to create the new buffer. Related to [#5093] Motivation: ByteBuf.readBytes(...) uses Unpooled.buffer(...) internally which will use a heap ByteBuf and also not able to make use of the allocator which may be pooled. We should better make use of the allocator. Modifications: Use the allocator for thenew buffer. Result: Take allocator into account when copy bytes. --- .../java/io/netty/buffer/AbstractByteBuf.java | 3 +-- .../io/netty/buffer/AbstractByteBufTest.java | 16 ++++++++++++++++ .../java/io/netty/buffer/SlicedByteBufTest.java | 8 +++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java index 34ccab7a85..0ea4c420a0 100644 --- a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java @@ -674,8 +674,7 @@ public abstract class AbstractByteBuf extends ByteBuf { return Unpooled.EMPTY_BUFFER; } - // Use an unpooled heap buffer because there's no way to mandate a user to free the returned buffer. - ByteBuf buf = Unpooled.buffer(length, maxCapacity); + ByteBuf buf = alloc().buffer(length, maxCapacity); buf.writeBytes(this, readerIndex, length); readerIndex += length; return buf; diff --git a/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java index c1ae156ab6..3605d11d63 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java @@ -1227,6 +1227,7 @@ public abstract class AbstractByteBufTest { // Make sure if it is a copied buffer. actualValue.setByte(0, (byte) (actualValue.getByte(0) + 1)); assertFalse(buffer.getByte(i) == actualValue.getByte(0)); + actualValue.release(); } } @@ -2523,6 +2524,21 @@ public abstract class AbstractByteBufTest { assertEquals(0, readOnlyDst.position()); } + @Test + public void testReadBytes() { + ByteBuf buffer = newBuffer(8); + byte[] bytes = new byte[8]; + buffer.writeBytes(bytes); + + ByteBuf buffer2 = buffer.readBytes(4); + assertSame(buffer.alloc(), buffer2.alloc()); + assertEquals(4, buffer.readerIndex()); + assertTrue(buffer.release()); + assertEquals(0, buffer.refCnt()); + assertTrue(buffer2.release()); + assertEquals(0, buffer2.refCnt()); + } + private void testRefCnt0(final boolean parameter) throws Exception { for (int i = 0; i < 10; i++) { final CountDownLatch latch = new CountDownLatch(1); diff --git a/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java b/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java index 88c6e56ca7..0e06307df3 100644 --- a/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java @@ -117,7 +117,13 @@ public class SlicedByteBufTest extends AbstractByteBufTest { @Test @Override public void testLittleEndianWithExpand() { - // ignore for SlicedByteBuf + // ignore for SlicedByteBuf + } + + @Test + @Override + public void testReadBytes() { + // ignore for SlicedByteBuf } @Test