class DynamicCompositeByteBuf {

    public ByteBuf readRetainedSlice(int length) {
        if (length > readableBytes) {
            throw new IndexOutOfBoundsException("length > readableBytes");
        }
        if (length <= 0) {
            return Unpooled.buffer(0);
        }

        ByteBuf first = buffers.peek();
        int firstLen = first.readableBytes();
        if (length == firstLen) {
            readableBytes -= length;
            return buffers.removeFirst();
        } else if (length < firstLen) {
            ByteBuf newBuf = first.readRetainedSlice(length);
            readableBytes -= length;
            return newBuf;
        } else {
            int capacity = 2;
            ByteBuf[] byteBufs = new ByteBuf[capacity];
            int i = 0;
            while (length > 0 && readableBytes > 0) {
                ByteBuf newBuf;
                if (firstLen > length) {
                    newBuf = first.readRetainedSlice(length);
                    readableBytes -= length;
                    length = 0;
                } else {
                    newBuf = first;
                    readableBytes -= firstLen;
                    length -= firstLen;
                    buffers.pop();
                }
                if (i == capacity) {
                    int newCapacity = capacity * 2;
                    ByteBuf[] newByteBufs = new ByteBuf[newCapacity];
                    System.arraycopy(byteBufs, 0, newByteBufs, 0, capacity);
                    byteBufs = newByteBufs;
                    capacity = newCapacity;
                }
                byteBufs[i++] = newBuf;
                first = buffers.peek();
                if (first == null) {
                    break;
                }
                firstLen = first.readableBytes();
            }
            if (i == capacity) {
                int maxComponentNum = (i > 2) ? i : 2;
                return new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, true, maxComponentNum, byteBufs);
            } else {
                ByteBuf[] outBufs = new ByteBuf[i];
                System.arraycopy(byteBufs, 0, outBufs, 0, i);
                int maxComponentNum = (i > 2) ? i : 2;
                return new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, true, maxComponentNum, outBufs);
            }
        }
    }

    public  DynamicCompositeByteBuf();
    public  DynamicCompositeByteBuf(int capacity);
    public  DynamicCompositeByteBuf(ByteBuf buf);
    public  DynamicCompositeByteBuf(ByteBuf[] bufs, int offset, int len);

}