diff --git a/buffer/src/main/java/io/netty/buffer/FixedCompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/FixedCompositeByteBuf.java index 816c0b41de..af28d682d8 100644 --- a/buffer/src/main/java/io/netty/buffer/FixedCompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/FixedCompositeByteBuf.java @@ -343,7 +343,7 @@ final class FixedCompositeByteBuf extends AbstractReferenceCountedByteBuf { int adjustment = c.offset; ByteBuf s = c.buf; for (;;) { - int localLength = Math.min(length, s.capacity() - (index - adjustment)); + int localLength = Math.min(length, s.readableBytes() - (index - adjustment)); dst.limit(dst.position() + localLength); s.getBytes(index - adjustment, dst); index += localLength; @@ -372,7 +372,7 @@ final class FixedCompositeByteBuf extends AbstractReferenceCountedByteBuf { int adjustment = c.offset; ByteBuf s = c.buf; for (;;) { - int localLength = Math.min(length, s.capacity() - (index - adjustment)); + int localLength = Math.min(length, s.readableBytes() - (index - adjustment)); s.getBytes(index - adjustment, dst, dstIndex, localLength); index += localLength; dstIndex += localLength; @@ -414,7 +414,7 @@ final class FixedCompositeByteBuf extends AbstractReferenceCountedByteBuf { int adjustment = c.offset; ByteBuf s = c.buf; for (;;) { - int localLength = Math.min(length, s.capacity() - (index - adjustment)); + int localLength = Math.min(length, s.readableBytes() - (index - adjustment)); s.getBytes(index - adjustment, out, localLength); index += localLength; length -= localLength; @@ -491,7 +491,7 @@ final class FixedCompositeByteBuf extends AbstractReferenceCountedByteBuf { int adjustment = c.offset; ByteBuf s = c.buf; for (;;) { - int localLength = Math.min(length, s.capacity() - (index - adjustment)); + int localLength = Math.min(length, s.readableBytes() - (index - adjustment)); switch (s.nioBufferCount()) { case 0: throw new UnsupportedOperationException(); diff --git a/buffer/src/test/java/io/netty/buffer/FixedCompositeByteBufTest.java b/buffer/src/test/java/io/netty/buffer/FixedCompositeByteBufTest.java index 7dea5ebf56..66cab63b8f 100644 --- a/buffer/src/test/java/io/netty/buffer/FixedCompositeByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/FixedCompositeByteBufTest.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ReadOnlyBufferException; import java.nio.channels.ScatteringByteChannel; +import java.nio.charset.Charset; import static io.netty.buffer.Unpooled.*; import static io.netty.util.ReferenceCountUtil.*; @@ -252,4 +253,65 @@ public class FixedCompositeByteBufTest { buf.release(); } + + @Test + public void testCopyingToOtherBuffer() { + ByteBuf buf1 = directBuffer(10); + ByteBuf buf2 = buffer(10); + ByteBuf buf3 = directBuffer(10); + buf1.writeBytes("a".getBytes(Charset.defaultCharset())); + buf2.writeBytes("b".getBytes(Charset.defaultCharset())); + buf3.writeBytes("c".getBytes(Charset.defaultCharset())); + ByteBuf composite = unmodifiableBuffer(buf1, buf2, buf3); + ByteBuf copy = directBuffer(3); + ByteBuf copy2 = buffer(3); + copy.setBytes(0, composite, 0, 3); + copy2.setBytes(0, composite, 0, 3); + copy.writerIndex(3); + copy2.writerIndex(3); + assertEquals(0, ByteBufUtil.compare(copy, composite)); + assertEquals(0, ByteBufUtil.compare(copy2, composite)); + assertEquals(0, ByteBufUtil.compare(copy, copy2)); + copy.release(); + copy2.release(); + composite.release(); + } + + @Test + public void testCopyingToOutputStream() throws IOException { + ByteBuf buf1 = directBuffer(10); + ByteBuf buf2 = buffer(10); + ByteBuf buf3 = directBuffer(10); + buf1.writeBytes("a".getBytes(Charset.defaultCharset())); + buf2.writeBytes("b".getBytes(Charset.defaultCharset())); + buf3.writeBytes("c".getBytes(Charset.defaultCharset())); + ByteBuf composite = unmodifiableBuffer(buf1, buf2, buf3); + ByteBuf copy = directBuffer(3); + ByteBuf copy2 = buffer(3); + composite.getBytes(0, new ByteBufOutputStream(copy), 3); + composite.getBytes(0, new ByteBufOutputStream(copy2), 3); + assertEquals(0, ByteBufUtil.compare(copy, composite)); + assertEquals(0, ByteBufUtil.compare(copy2, composite)); + assertEquals(0, ByteBufUtil.compare(copy, copy2)); + copy.release(); + copy2.release(); + composite.release(); + } + + @Test + public void testExtractNioBuffers() { + ByteBuf buf1 = directBuffer(10); + ByteBuf buf2 = buffer(10); + ByteBuf buf3 = directBuffer(10); + buf1.writeBytes("a".getBytes(Charset.defaultCharset())); + buf2.writeBytes("b".getBytes(Charset.defaultCharset())); + buf3.writeBytes("c".getBytes(Charset.defaultCharset())); + ByteBuf composite = unmodifiableBuffer(buf1, buf2, buf3); + ByteBuffer[] byteBuffers = composite.nioBuffers(0, 3); + assertEquals(3, byteBuffers.length); + assertEquals(1, byteBuffers[0].limit()); + assertEquals(1, byteBuffers[1].limit()); + assertEquals(1, byteBuffers[2].limit()); + composite.release(); + } }