diff --git a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java index 2dbf4333d7..e45575dea1 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 boolean direct; private final List components = new ArrayList(); private final int maxNumComponents; + private static final ByteBuffer FULL_BYTEBUFFER = (ByteBuffer) ByteBuffer.allocate(1).position(1); private boolean freed; @@ -698,6 +699,10 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf { @Override public CompositeByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.capacity()); + if (length == 0) { + return this; + } + int i = toComponentIndex(index); while (length > 0) { Component c = components.get(i); @@ -842,6 +847,9 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf { @Override public CompositeByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { checkSrcIndex(index, length, srcIndex, src.length); + if (length == 0) { + return this; + } int i = toComponentIndex(index); while (length > 0) { @@ -864,6 +872,10 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf { int length = src.remaining(); checkIndex(index, length); + if (length == 0) { + return this; + } + int i = toComponentIndex(index); try { while (length > 0) { @@ -886,6 +898,9 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf { @Override public CompositeByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { checkSrcIndex(index, length, srcIndex, src.capacity()); + if (length == 0) { + return this; + } int i = toComponentIndex(index); while (length > 0) { @@ -905,6 +920,9 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf { @Override public int setBytes(int index, InputStream in, int length) throws IOException { checkIndex(index, length); + if (length == 0) { + return in.read(EmptyArrays.EMPTY_BYTES); + } int i = toComponentIndex(index); int readBytes = 0; @@ -941,6 +959,9 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf { @Override public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { checkIndex(index, length); + if (length == 0) { + return in.read(FULL_BYTEBUFFER); + } int i = toComponentIndex(index); int readBytes = 0; @@ -982,7 +1003,9 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf { public ByteBuf copy(int index, int length) { checkIndex(index, length); ByteBuf dst = Unpooled.buffer(length); - copyTo(index, length, toComponentIndex(index), dst); + if (length != 0) { + copyTo(index, length, toComponentIndex(index), dst); + } return dst; } diff --git a/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java index 6639f7a606..5bcae43aba 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java @@ -552,4 +552,18 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest { buf.removeComponent(0); assertEquals(0, buf.numComponents()); } + + @Test + public void testCopyEmpty() { + CompositeByteBuf buf = freeLater(compositeBuffer()); + assertEquals(0, buf.numComponents()); + assertEquals(0, freeLater(buf.copy()).readableBytes()); + } + + @Test + public void testDuplicateEmpty() { + CompositeByteBuf buf = freeLater(compositeBuffer()); + assertEquals(0, buf.numComponents()); + assertEquals(0, freeLater(buf.duplicate()).readableBytes()); + } }