public void flush(final DataStreamChannel channel) throws IOException {
        lock.lock();
        try {
            dataStreamChannel = channel;
            hasCapacity = true;
            setOutputMode();
            if (buffer().hasRemaining()) {
                dataStreamChannel.write(buffer());
            }
            if (!buffer().hasRemaining() && endStream) {
                propagateEndStream();
            }
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }
TODO: to be replaced by core functionality
httpclient5/src/main/java/org/apache/hc/client5/http/impl/compat/SharedOutputBuffer.java