diff --git a/transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java b/transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java index d921ae9bdd..c54560c66b 100644 --- a/transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java +++ b/transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java @@ -451,7 +451,7 @@ public final class ChannelOutboundBuffer { // branch is not very likely to get hit very frequently. nioBufferCount = nioBuffers(entry, buf, nioBuffers, nioBufferCount, maxCount); } - if (nioBufferCount == maxCount) { + if (nioBufferCount >= maxCount) { break; } } diff --git a/transport/src/test/java/io/netty/channel/ChannelOutboundBufferTest.java b/transport/src/test/java/io/netty/channel/ChannelOutboundBufferTest.java index fccd4e7421..8d74325742 100644 --- a/transport/src/test/java/io/netty/channel/ChannelOutboundBufferTest.java +++ b/transport/src/test/java/io/netty/channel/ChannelOutboundBufferTest.java @@ -120,6 +120,31 @@ public class ChannelOutboundBufferTest { buf.release(); } + @Test + public void testNioBuffersMaxCount() { + TestChannel channel = new TestChannel(); + + ChannelOutboundBuffer buffer = new ChannelOutboundBuffer(channel); + + CompositeByteBuf comp = compositeBuffer(256); + ByteBuf buf = directBuffer().writeBytes("buf1".getBytes(CharsetUtil.US_ASCII)); + for (int i = 0; i < 65; i++) { + comp.addComponent(true, buf.copy()); + } + assertEquals(65, comp.nioBufferCount()); + buffer.addMessage(comp, comp.readableBytes(), channel.voidPromise()); + assertEquals("Should still be 0 as not flushed yet", 0, buffer.nioBufferCount()); + buffer.addFlush(); + final int maxCount = 10; // less than comp.nioBufferCount() + ByteBuffer[] buffers = buffer.nioBuffers(maxCount, Integer.MAX_VALUE); + assertTrue("Should not be greater than maxCount", buffer.nioBufferCount() <= maxCount); + for (int i = 0; i < buffer.nioBufferCount(); i++) { + assertEquals(buffers[i], buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes())); + } + release(buffer); + buf.release(); + } + private static void release(ChannelOutboundBuffer buffer) { for (;;) { if (!buffer.remove()) {