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