class StoreResponseStreamSinkConduit extends AbstractStreamSinkConduit<StreamSinkConduit> {

    public static final AttachmentKey<byte[]> RESPONSE = AttachmentKey.create(byte[].class);
    private ByteArrayOutputStream outputStream;
    private final HttpServerExchange exchange;

    public StoreResponseStreamSinkConduit(StreamSinkConduit next, HttpServerExchange exchange) {
        super(next);
        this.exchange = exchange;
        long length = exchange.getResponseContentLength();
        if (length <= 0L) {
            outputStream = new ByteArrayOutputStream();
        } else {
            if (length > Integer.MAX_VALUE) {
                throw UndertowMessages.MESSAGES.responseTooLargeToBuffer(length);
            }
            outputStream = new ByteArrayOutputStream((int) length);
        }
    }

    @Override
    public int write(ByteBuffer src) throws IOException {
        int start = src.position();
        int ret = super.write(src);
        for (int i = start; i < start + ret; ++i) {
            outputStream.write(src.get(i));
        }
        return ret;
    }

    @Override
    public long write(ByteBuffer[] srcs, int offs, int len) throws IOException {<FILL_FUNCTION_BODY>}

    @Override
    public int writeFinal(ByteBuffer src) throws IOException {
        int start = src.position();
        int ret = super.writeFinal(src);
        //without changing ByteBuffer remaining, copy to outputStream
        for (int i = start; i < start + ret; ++i) {
            outputStream.write(src.get(i));
        }
        return ret;
    }

    @Override
    public long writeFinal(ByteBuffer[] srcs, int offs, int len) throws IOException {
        int[] starts = new int[len];
        long toWrite = 0;
        for (int i = 0; i < len; ++i) {
            starts[i] = srcs[i + offs].position();
            toWrite += srcs[i + offs].remaining();
        }
        long ret = super.write(srcs, offs, len);
        long rem = ret;

        for (int i = 0; i < len; ++i) {
            ByteBuffer buf = srcs[i + offs];
            int pos = starts[i];
            int limit = buf.limit();
            //buf.position() could be later than buf.limit() due to after read the last byte, position will still move to next, but it's now larger than limit.
            while (rem > 0 && pos <= buf.position() && pos < limit) {
                outputStream.write(buf.get(pos));
                pos++;
                rem--;
            }
        }
        return ret;
    }

    @Override
    public void terminateWrites() throws IOException {
        //after finish writes all through conduit, it will reach here, at this time, we put response info
        exchange.putAttachment(RESPONSE, outputStream.toByteArray());
        outputStream = null;
        super.terminateWrites();
    }
}

        int[] starts = new int[len];
        for (int i = 0; i < len; ++i) {
            starts[i] = srcs[i + offs].position();
        }
        long ret = super.write(srcs, offs, len);
        long rem = ret;

        for (int i = 0; i < len; ++i) {
            ByteBuffer buf = srcs[i + offs];
            int pos = starts[i];
            int limit = buf.limit();
            //buf.position() could be later than buf.limit() due to after read the last byte, position will still move to next, but it's now larger than limit.
            while (rem > 0 && pos <= buf.position() && pos < limit) {
                outputStream.write(buf.get(pos));
                pos++;
                rem--;
            }
        }
        return ret;
    
<no_super_class>