class SubscriberInputStream extends InputStream implements Flow.Subscriber<ByteBuffer> {

    @Override
    public int read() throws IOException {
        try {
            while (true) {
                final ByteBuffer currentBuffer = processed.get(); // block until we have a buffer
                if (currentBuffer != null) {
                    if (currentBuffer.remaining() > 0) {
                        // There's at least one more byte, return it
                        return currentBuffer.get();
                    } else {
                        // We've finished with the current buffer; if we're still open, reinitialize the future
                        // and request more data
                        if (reinitializeFuture()) {
                            subscription.request(1);
                        } else {
                            // We're closed
                            return -1;
                        }
                    }
                } else {
                    // We're closed
                    return -1;
                }
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IOException(e);
        } catch (ExecutionException e) {
            throw new IOException(e.getCause());
        }
    }

}