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

Code before:
                }
            }
            throw iae;
        }

        void add(Buffer buffer) {
            if (index == array.length) {
                array = Arrays.copyOf(array, array.length * 2);
            }
            array[index] = buffer;
            index++;
        }

        void collect(Iterable<Buffer> externals) {
            for (Buffer buf : externals) {
                if (buf.capacity() == 0) {
                    buf.close();
                    continue;
                }
                if (CompositeBuffer.isComposite(buf)) {
                    CompositeBuffer cbuf = (CompositeBuffer) buf;
                    collect(Arrays.asList(cbuf.decomposeBuffer()));
                    continue;
                }
                add(buf);
            }
        }

        Buffer[] toArray() {
            int firstReadable = -1;
            int lastReadable = -1;
            for (int i = 0; i < index; i++) {
                if (array[i].readableBytes() != 0) {
                    if (firstReadable == -1) {
                        firstReadable = i;
                    }
                    lastReadable = i;
                }
            }
            // Bytes already read, in components after the first readable section, must be trimmed off.
            // Likewise, writable bytes prior to the last readable section must be trimmed off.
            if (firstReadable != -1) {
                // Remove middle buffers entirely that have no readable bytes.
                for (int i = firstReadable + 1; i < lastReadable; i++) {
                    Buffer buf = array[i];
                    if (buf.readableBytes() == 0) {
                        buf.close();
                        if (i <= index - 2) {
                            System.arraycopy(array, i + 1, array, i, index - i - 1);
                        }
                        i--;

Review comment (refactor):
Nit: I think this is a bit easier to read:


```suggestion
            for (Buffer buf : externals) {
                if (buf.capacity() == 0) {
                    buf.close();
                } else  if (CompositeBuffer.isComposite(buf)) {
                    CompositeBuffer cbuf = (CompositeBuffer) buf;
                    collect(Arrays.asList(cbuf.decomposeBuffer()));
                } else {
                    add(buf);
                }
            }
```

Code after:
            IllegalArgumentException iae = new IllegalArgumentException("Cannot compose duplicate buffers.");
            for (Buffer toClose : externals) {
                try {
                    toClose.close();
                } catch (Exception closeExc) {
                    iae.addSuppressed(closeExc);
                }
            }
            throw iae;
        }

        void add(Buffer buffer) {
            if (index == array.length) {
                array = Arrays.copyOf(array, array.length * 2);
            }
            array[index] = buffer;
            index++;
        }

        void collect(Iterable<Buffer> externals) {
            for (Buffer buf : externals) {
                if (buf.capacity() == 0) {
                    buf.close();
                } else if (CompositeBuffer.isComposite(buf)) {
                    CompositeBuffer cbuf = (CompositeBuffer) buf;
                    collect(Arrays.asList(cbuf.decomposeBuffer()));
                } else {
                    add(buf);
                }
            }
        }

        Buffer[] toArray() {
            int firstReadable = -1;
            int lastReadable = -1;
            for (int i = 0; i < index; i++) {
                if (array[i].readableBytes() != 0) {
                    if (firstReadable == -1) {
                        firstReadable = i;
                    }
                    lastReadable = i;
                }
            }
            // Bytes already read, in components after the first readable section, must be trimmed off.
            // Likewise, writable bytes prior to the last readable section must be trimmed off.
            if (firstReadable != -1) {
                // Remove middle buffers entirely that have no readable bytes.
                for (int i = firstReadable + 1; i < lastReadable; i++) {
                    Buffer buf = array[i];
                    if (buf.readableBytes() == 0) {
                        buf.close();