RxJava:如何从控制台以交互方式处理Flowable

时间:2019-11-20 19:39:09

标签: java console rx-java backpressure

我已经创建了可解析文件的Flowable(RxJava v3)。我希望它支持背压。这很重要,因为文件可能很大,并且我不希望它们立即加载到内存中。这是我的第一次尝试:

public Flowable<Byte> asFlowable(InputStream is) {
    return Flowable.create(new FlowableOnSubscribe<Byte>() {

         @Override
         public void subscribe(FlowableEmitter<Byte> emitter) throws Exception {
             try (DataInputStream inputStream = new DataInputStream(is)){
                 if (inputStream.readInt() != SOME_CONSTANT) {
                     throw new IllegalArgumentException("illegal file format");
                 }

                 if (inputStream.readInt() != SOME_OTHER_CONSTANT) {
                     throw new IllegalArgumentException("illegal file format");
                 }

                 final int numItems = inputStream.readInt();

                 for(int i = 0; i < numItems; i++) {
                     if(emitter.isCancelled()) {
                         return;
                     }

                     emitter.onNext(inputStream.readByte());
                 }

                 emitter.onComplete();       
             } catch (Exception e) {
                 emitter.onError(e);
             } 
         }
     }, BackpressureStrategy.BUFFER);
}

我使用Flowable.create而不是Flowable.generate的原因是因为我需要验证文件,并且如果文件开头的某些幻数错误或找不到,则会引发错误。这与Flowable.generate lambda不太匹配(但是如果您知道更好的方法,请发布它)。

好吧,我们假设冷的Flowable支持背压。现在,我想在类似控制台的应用程序中对其进行处理。

问题: 我想从Flowable请求一个新的字节,并在用户每次按下空格时将其打印以进行控制台(类似于Linux中的moreless)。最好的方法是什么?我打算直接在public static void main方法中观察flowable,因为我需要使用控制台进行读写。

我一直在阅读Backpressure section in RxJAva's Wiki并发现以下代码段:

someObservable.subscribe(new Subscriber<t>() {
    @Override
    public void onStart() {
      request(1);
    }

    @Override
    public void onCompleted() {
      // gracefully handle sequence-complete
    }

    @Override
    public void onError(Throwable e) {
      // gracefully handle error
    }

    @Override
    public void onNext(t n) {
      // do something with the emitted item "n"
      // request another item:
      request(1);
    }
});

但这让我更加困惑,因为request方法似乎在RxJava 3中不存在。

1 个答案:

答案 0 :(得分:2)

使用generateblockingSubscribe并从控制台读取一行:

class State {
     DataInputStream inputStream;
     int count;
     int i;
}

BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));

Flowable.generate(() -> {
    State s = new State();
    s.inputStream = new DataInputStream(is);
    try {
        if (s.inputStream.readInt() != SOME_CONSTANT) {
            throw new IllegalArgumentException("illegal file format");
        }

        if (s.inputStream.readInt() != SOME_OTHER_CONSTANT) {
            throw new IllegalArgumentException("illegal file format");
        }
        s.count = s.inputStream.readInt();
    } catch (IOException ex) {
        s.inputStream.close();
        throw ex;
    }
    return s;
}, (state, emitter) -> {
    if (state.i < s.count) {
        emitter.onNext(state.inputStream.readByte());
        s.i++;
    }
    if (state.i >= s.count) {
        emitter.onComplete();
    }
}, state -> {
    state.inputStream.close();
})
.subscribeOn(Schedulers.io())
.blockingSubscribe(b -> {
    System.out.println(b);
    bin.readLine();
}, Flowable.bufferSize());