From 66294892a0eb7ec5c28bb75e173de205ea86100b Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 25 Nov 2014 18:30:53 +0100 Subject: [PATCH] CompositeByteBuf.nioBuffers(...) must not return an empty ByteBuffer array Motivation: CompositeByteBuf.nioBuffers(...) returns an empty ByteBuffer array if the specified length is 0. This is not consistent with other ByteBuf implementations which return an ByteBuffer array of size 1 with an empty ByteBuffer included. Modifications: Make CompositeByteBuf.nioBuffers(...) consistent with other ByteBuf implementations. Result: Consistent and correct behaviour of nioBufffers(...) --- .../main/java/io/netty/buffer/CompositeByteBuf.java | 3 ++- .../test/java/io/netty/buffer/AbstractByteBufTest.java | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java index 471b6cb77d..b88da0c779 100644 --- a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java @@ -45,6 +45,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf { private final List components = new ArrayList(); private final int maxNumComponents; private static final ByteBuffer FULL_BYTEBUFFER = (ByteBuffer) ByteBuffer.allocate(1).position(1); + private static final ByteBuffer EMPTY_BYTEBUFFER = ByteBuffer.allocateDirect(0); private boolean freed; @@ -1137,7 +1138,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf { public ByteBuffer[] nioBuffers(int index, int length) { checkIndex(index, length); if (length == 0) { - return EmptyArrays.EMPTY_BYTE_BUFFERS; + return new ByteBuffer[] { EMPTY_BYTEBUFFER }; } List buffers = new ArrayList(components.size()); diff --git a/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java index 0eabfef988..684bbb2042 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java @@ -2486,6 +2486,16 @@ public abstract class AbstractByteBufTest { testRefCnt0(true); } + @Test + public void testEmptyNioBuffers() throws Exception { + ByteBuf buffer = releaseLater(newBuffer(8)); + buffer.clear(); + assertFalse(buffer.isReadable()); + ByteBuffer[] nioBuffers = buffer.nioBuffers(); + assertEquals(1, nioBuffers.length); + assertFalse(nioBuffers[0].hasRemaining()); + } + private void testRefCnt0(final boolean parameter) throws Exception { for (int i = 0; i < 10; i++) { final CountDownLatch latch = new CountDownLatch(1);