diff --git a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java index 7afff7e7f1..bcd5f0a564 100644 --- a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java @@ -61,7 +61,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements this.alloc = alloc; this.direct = direct; this.maxNumComponents = maxNumComponents; - components = newList(maxNumComponents); + components = newList(0, maxNumComponents); } public CompositeByteBuf(ByteBufAllocator alloc, boolean direct, int maxNumComponents, ByteBuf... buffers) { @@ -82,7 +82,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements this.alloc = alloc; this.direct = direct; this.maxNumComponents = maxNumComponents; - components = newList(maxNumComponents); + components = newList(len, maxNumComponents); addComponents0(false, 0, buffers, offset, len); consolidateIfNeeded(); @@ -100,18 +100,21 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements "maxNumComponents: " + maxNumComponents + " (expected: >= 2)"); } + int len = buffers instanceof Collection ? ((Collection) buffers).size() : 0; + this.alloc = alloc; this.direct = direct; this.maxNumComponents = maxNumComponents; - components = newList(maxNumComponents); + components = newList(len, maxNumComponents); addComponents0(false, 0, buffers); consolidateIfNeeded(); setIndex(0, capacity()); } - private static ComponentList newList(int maxNumComponents) { - return new ComponentList(Math.min(AbstractByteBufAllocator.DEFAULT_MAX_COMPONENTS, maxNumComponents)); + private static ComponentList newList(int initComponents, int maxNumComponents) { + int capacityGuess = Math.min(AbstractByteBufAllocator.DEFAULT_MAX_COMPONENTS, maxNumComponents); + return new ComponentList(Math.max(initComponents, capacityGuess)); } // Special constructor used by WrappedCompositeByteBuf diff --git a/buffer/src/main/java/io/netty/buffer/Unpooled.java b/buffer/src/main/java/io/netty/buffer/Unpooled.java index 3639a17768..8755989782 100644 --- a/buffer/src/main/java/io/netty/buffer/Unpooled.java +++ b/buffer/src/main/java/io/netty/buffer/Unpooled.java @@ -238,7 +238,7 @@ public final class Unpooled { * content will be visible to the returned buffer. */ public static ByteBuf wrappedBuffer(byte[]... arrays) { - return wrappedBuffer(AbstractByteBufAllocator.DEFAULT_MAX_COMPONENTS, arrays); + return wrappedBuffer(arrays.length, arrays); } /** @@ -249,7 +249,7 @@ public final class Unpooled { * @return The readable portion of the {@code buffers}. The caller is responsible for releasing this buffer. */ public static ByteBuf wrappedBuffer(ByteBuf... buffers) { - return wrappedBuffer(AbstractByteBufAllocator.DEFAULT_MAX_COMPONENTS, buffers); + return wrappedBuffer(buffers.length, buffers); } /** @@ -258,7 +258,7 @@ public final class Unpooled { * specified buffers will be visible to the returned buffer. */ public static ByteBuf wrappedBuffer(ByteBuffer... buffers) { - return wrappedBuffer(AbstractByteBufAllocator.DEFAULT_MAX_COMPONENTS, buffers); + return wrappedBuffer(buffers.length, buffers); } /**