diff --git a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java index 50aa120a09..0017cd5f87 100644 --- a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java @@ -1122,7 +1122,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf { //noinspection ForLoopReplaceableByForEach for (int i = 0; i < buffers.length; i++) { - merged.put(buffers[0]); + merged.put(buffers[i]); } merged.flip(); diff --git a/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java index 350ef23ce0..8b4ac0c952 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java @@ -176,6 +176,27 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest { assertEquals(0, buf.arrayOffset()); } + @Test + public void testCompositeToSingleBuffer() { + CompositeByteBuf buf = compositeBuffer(3); + + buf.addComponent(wrappedBuffer(new byte[] {1, 2, 3})); + assertEquals(1, buf.numComponents()); + + buf.addComponent(wrappedBuffer(new byte[] {4})); + assertEquals(2, buf.numComponents()); + + buf.addComponent(wrappedBuffer(new byte[] {5, 6})); + assertEquals(3, buf.numComponents()); + + // NOTE: hard-coding 6 here, since it seems like addComponent doesn't bump the writer index. + // I'm unsure as to whether or not this is correct behavior + ByteBuffer nioBuffer = buf.nioBuffer(0, 6); + byte[] bytes = nioBuffer.array(); + assertEquals(6, bytes.length); + assertArrayEquals(new byte[] {1, 2, 3, 4, 5, 6}, bytes); + } + @Test public void testFullConsolidation() { CompositeByteBuf buf = freeLater(compositeBuffer(Integer.MAX_VALUE));