Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a file asynchronously with RxJava 2

I'm trying to create an Observable<ByteBuffer> or a Flowable<ByteBuffer> which would read a file asynchronously (or at least I'm looking for the best performance Java could give me in that use case - reading a file).

I want to read it part by part (filling each time a new ByteBuffer), because I don't have enough RAM to store it entirely in memory, and want backpressure handling (because the ByteBuffers must be treated one by one, so I don't want the IO to overflow the computing).

I'm a beginner at Reactive Programming and so am I at RxJava. So perhaps it already exists some libraries that does exactly what I want? (I've already searched for it but didn't find yet)

If this is not the case, could someone tell me how to do what I want please?

like image 330
Anthony O. Avatar asked Apr 21 '26 04:04

Anthony O.


1 Answers

It's not an easy task to create a publisher and conform to the reactive-streams specification https://github.com/reactive-streams/reactive-streams-jvm/blob/v1.0.3/README.md#specification

You can try https://github.com/cqfn/rio library for that:

Publisher<ByteBuffer> pub = new File(Path.of("/tmp/file")).content();

If you want to implement it by yourself, you can check sources of reactive Channel implementation: https://github.com/cqfn/rio/blob/master/src/main/java/org/cqfn/rio/channel/ReadableChannelPublisher.java

like image 91
Kirill Avatar answered Apr 23 '26 16:04

Kirill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!