/** * Creates a {@link Publisher} emitting {@link DataBuffer}s by reading binary chunks from {@link AsyncInputStream}. * Closes the {@link AsyncInputStream} once the {@link Publisher} terminates. * * @param inputStream must not be {@literal null}. * @param dataBufferFactory must not be {@literal null}. * @return the resulting {@link Publisher}. */ static Flux<DataBuffer> createBinaryStream(AsyncInputStream inputStream, DataBufferFactory dataBufferFactory) { State state = new State(inputStream, dataBufferFactory); return Flux.usingWhen(Mono.just(inputStream), it -> { return Flux.<DataBuffer> create((sink) -> { sink.onDispose(state::close); sink.onCancel(state::close); sink.onRequest(n -> { state.request(sink, n); }); }); }, AsyncInputStream::close, AsyncInputStream::close, AsyncInputStream::close) // .concatMap(Flux::just, 1); }