简体   繁体   中英

Get an Publisher<ByteBuffer> from InputStream

I just upgraded my mongo-db-java-driver and now the handy function GridFSBucket.uploadFromStream has gone. Therefore we now got a

GridFSUploadPublisher<ObjectId> uploadFromPublisher(String filename, Publisher<ByteBuffer> source);

Any ideas how to convert my InputStream into an Publisher<ByteBuffer> ? Is there any utilfunction in the java driver or Reactor?

There is a util in spring framework to convert input stream to data buffer flux. And it's direct to map data buffer as byte buffer.

Flux<ByteBuffer> byteBufferFlux = DataBufferUtils.readByteChannel(() -> Channels.newChannel(inputStream), DefaultDataBufferFactory.sharedInstance, 4096).map(DataBuffer::asByteBuffer);

To just make it work one could read stream and store data in byte[] array which later can be wrapped with ByteBuffer.wrap(byte[]) method. I've looked into older MongoDB driver's source code and found stream to be used as actual stream, so it was not sneakily converted to ByteBuffer under the hood back then.

It seems there is no method to stream the data as it comes, there is no other way than to load file into memory now. I can see how it can be a problem in many scenarios, but maybe I just can't understand something about this approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM