class PayloadOutputChannel implements AutoCloseable {
    /** Initial output stream buffer capacity. */
    private static final int INITIAL_BUFFER_CAPACITY = 1024;

    /** Client channel. */
    private final ClientChannel ch;

    /** Output stream. */
    private final BinaryHeapOutputStream out;

    /** Close guard. */
    private final AtomicBoolean closed = new AtomicBoolean();

    /**
     * Constructor.
     */
    PayloadOutputChannel(ClientChannel ch) {
        // Disable AutoCloseable on the stream so that out callers don't release the pooled buffer before it is written to the socket.
        out = new BinaryHeapOutputStream(INITIAL_BUFFER_CAPACITY, BinaryMemoryAllocator.POOLED.chunk(), true);
        this.ch = ch;
    }

    /**
     * Gets client channel.
     */
    public ClientChannel clientChannel() {
        return ch;
    }

    /**
     * Gets output stream.
     */
    public BinaryOutputStream out() {
        return out;
    }

    /** {@inheritDoc} */
    @Override public void close() {<FILL_FUNCTION_BODY>}
}

        // Pooled buffer is reusable and should be released once and only once.
        // Releasing more than once potentially "steals" it from another request.
        if (closed.compareAndSet(false, true))
            out.release();
    
<no_super_class>