Example usage for io.vertx.core.streams ReadStream handler

List of usage examples for io.vertx.core.streams ReadStream handler

Introduction

In this page you can find the example usage for io.vertx.core.streams ReadStream handler.

Prototype

@Fluent
ReadStream<T> handler(@Nullable Handler<T> handler);

Source Link

Document

Set a data handler.

Usage

From source file:org.sfs.io.AsyncIO.java

License:Apache License

public static <M> Observable<Void> pump(final ReadStream<M> rs, final EndableWriteStream<M> ws) {
    rs.pause();//  w w  w. ja  va  2 s .  c  om
    return Observable.defer(() -> {
        ObservableFuture<Void> observableFuture = RxHelper.observableFuture();
        Handler<Throwable> exceptionHandler = throwable -> {
            try {
                ws.drainHandler(null);
                rs.handler(null);
            } catch (Throwable e) {
                observableFuture.fail(e);
                return;
            }
            observableFuture.fail(throwable);
        };
        rs.exceptionHandler(exceptionHandler);
        ws.exceptionHandler(exceptionHandler);
        Handler<Void> drainHandler = event -> {
            try {
                rs.resume();
            } catch (Throwable e) {
                exceptionHandler.handle(e);
            }
        };

        Handler<M> dataHandler = data -> {
            try {
                ws.write(data);
                if (ws.writeQueueFull()) {
                    rs.pause();
                    ws.drainHandler(drainHandler);
                }
            } catch (Throwable e) {
                exceptionHandler.handle(e);
            }
        };
        ws.endHandler(observableFuture::complete);
        rs.endHandler(event -> {
            try {
                ws.end();
            } catch (Throwable e) {
                exceptionHandler.handle(e);
            }
        });
        try {
            rs.handler(dataHandler);
            rs.resume();
        } catch (Throwable e) {
            exceptionHandler.handle(e);
        }
        return observableFuture;
    });
}