Repository: netty/netty
File: buffer/src/main/java/io/netty5/buffer/api/DefaultCompositeBuffer.java (Java)

Code before:
    }

    /**
     * @see CompositeBuffer#compose(BufferAllocator)
     */
    public static CompositeBuffer compose(BufferAllocator allocator) {
        return new DefaultCompositeBuffer(allocator, EMPTY_BUFFER_ARRAY, COMPOSITE_DROP);
    }

    private static Buffer[] filterExternalBufs(Iterable<Buffer> externals) {
        // We filter out all zero-capacity buffers because they wouldn't contribute to the composite buffer anyway,
        // and also, by ensuring that all constituent buffers contribute to the size of the composite buffer,
        // we make sure the number of composite buffers will never become greater than the number of bytes in
        // the composite buffer.
        // We also filter out middle buffers that don't contribute any readable bytes, due to their trimming.
        // This restriction guarantees that methods like countComponents, forEachReadable and forEachWritable,
        // will never overflow their component counts.
        // Allocating a new array unconditionally also prevents external modification of the array.
        Collector collector = new Collector(externals);
        collector.collect(externals);
        return collector.toArray();
    }

    private static final class Collector {
        Buffer[] array;
        int index;

        Collector(Iterable<Buffer> externals) {
            var dupeCheck = new IdentityHashMap<>();
            int size = 0;
            for (Buffer buf : externals) {
                if (dupeCheck.put(buf, buf) != null) {
                    // Throw if there are duplicates among the buffers.
                    // We can do this before we decompose composites, because the components are owned,
                    // and no two composite buffers can have the same components, unless they violate
                    // their API contract.
                    closeAllAndThrowDupeException(externals);
                }
                size += buf.countComponents();
            }
            array = new Buffer[size];
        }

        private static void closeAllAndThrowDupeException(Iterable<Buffer> externals) {
            IllegalArgumentException iae = new IllegalArgumentException("Cannot compose duplicate buffers.");
            for (Buffer toClose : externals) {
                try {
                    toClose.close();
                } catch (Exception closeExc) {
                    iae.addSuppressed(closeExc);
                }

Review comment (suggestion):
```suggestion
        private Buffer[] array;
        private int index;
```

Code after:
        return new DefaultCompositeBuffer(allocator, filterExternalBufs(bufs), COMPOSITE_DROP);
    }

    /**
     * @see CompositeBuffer#compose(BufferAllocator)
     */
    public static CompositeBuffer compose(BufferAllocator allocator) {
        return new DefaultCompositeBuffer(allocator, EMPTY_BUFFER_ARRAY, COMPOSITE_DROP);
    }

    private static Buffer[] filterExternalBufs(Iterable<Buffer> externals) {
        // We filter out all zero-capacity buffers because they wouldn't contribute to the composite buffer anyway,
        // and also, by ensuring that all constituent buffers contribute to the size of the composite buffer,
        // we make sure the number of composite buffers will never become greater than the number of bytes in
        // the composite buffer.
        // We also filter out middle buffers that don't contribute any readable bytes, due to their trimming.
        // This restriction guarantees that methods like countComponents, forEachReadable and forEachWritable,
        // will never overflow their component counts.
        // Allocating a new array unconditionally also prevents external modification of the array.
        Collector collector = new Collector(externals);
        collector.collect(externals);
        return collector.toArray();
    }

    private static final class Collector {
        private Buffer[] array;
        private int index;

        Collector(Iterable<Buffer> externals) {
            final Map<Buffer, Buffer> dupeCheck;
            if (externals instanceof Collection) {
                dupeCheck = new IdentityHashMap<>(((Collection<?>) externals).size());
            } else {
                dupeCheck = new IdentityHashMap<>();
            }
            int size = 0;
            for (Buffer buf : externals) {
                if (dupeCheck.put(buf, buf) != null) {
                    // Throw if there are duplicates among the buffers.
                    // We can do this before we decompose composites, because the components are owned,
                    // and no two composite buffers can have the same components, unless they violate
                    // their API contract.
                    closeAllAndThrowDupeException(externals);
                }
                size += buf.countComponents();
            }
            array = new Buffer[size];
        }

        private static void closeAllAndThrowDupeException(Iterable<Buffer> externals) {
            IllegalArgumentException iae = new IllegalArgumentException("Cannot compose duplicate buffers.");