简体   繁体   中英

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 ByteBuffer s 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?

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

You can use Vert.x to do that as one of the available options. Sample code is provided below:

import io.vertx.core.file.OpenOptions;
import io.vertx.rxjava.core.Vertx;

--

public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    vertx.fileSystem()
            .openObservable("PATH-TO-FILE", new OpenOptions())
            .flatMap(asyncFile -> asyncFile.toObservable())
            .subscribe(buffer -> System.out.println(buffer.length() + "\n\n"), 
                       e-> e.printStackTrace(), 
                       () -> vertx.close());

}

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