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

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

Introduction

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

Prototype

@Fluent
ReadStream<T> endHandler(@Nullable Handler<Void> endHandler);

Source Link

Document

Set an end handler.

Usage

From source file:io.github.bckfnn.ftp.FtpClient.java

License:Apache License

/**
 * Perform a store (upload) to the file of path. 
 * @param path the path to upload to.//w  w  w. j a v a2  s  . c o  m
 * @param localFile an asyncFile where the download file will be written
 * @param progress a progress handler that is called for each part that is recieved.
 * @param handler callback handler that is called when the list is completed.
 */
public void stor(String path, ReadStream<Buffer> localFile, Handler<Progress> progress,
        Handler<AsyncResult<Void>> handler) {
    AtomicInteger ends = new AtomicInteger(0);
    pasv(handler, datasocket -> {
        localFile.endHandler($ -> {
            datasocket.close();
            if (ends.incrementAndGet() == 2) {
                handler.handle(Future.succeededFuture());
            }
        });
        new ProgressPump(localFile, datasocket, pumped -> {
            progress.handle(new Progress(this, pumped));
        }).start();
    }, $ -> {
        write("STOR " + path, resp(handler, when("125", "150", retr -> {
            handle(resp(handler, when("226", "250", listdone -> {
                if (ends.incrementAndGet() == 2) {
                    handler.handle(Future.succeededFuture());
                }
            })));
        })));
    });
}

From source file:org.apache.servicecomb.foundation.vertx.stream.PumpCommon.java

License:Apache License

/**
 *
 * @param context//ww w .  j  a v  a2 s. co  m
 * @param readStream
 * @param writeStream
 * @return future of save action<br>
 * <p>important:
 * <p>  if writeStream is AsyncCloseable, future means write complete
 * <p>  if writeStream is not AsyncCloseable, future only means read complete
 */
@SuppressWarnings("unchecked")
public CompletableFuture<Void> pump(Context context, ReadStream<Buffer> readStream,
        WriteStream<Buffer> writeStream) {
    CompletableFuture<Void> readFuture = new CompletableFuture<>();

    writeStream.exceptionHandler(readFuture::completeExceptionally);
    readStream.exceptionHandler(readFuture::completeExceptionally);
    // just means read finished, not means write finished
    readStream.endHandler(readFuture::complete);

    // if readStream(HttpClientResponse) and awriteStream(HttpServerResponse)
    // belongs to difference eventloop
    // maybe will cause deadlock
    // if happened, vertx will print deadlock stacks
    Pump.pump(readStream, writeStream).start();
    try {
        context.runOnContext(v -> readStream.resume());
    } catch (Throwable e) {
        readFuture.completeExceptionally(e);
    }

    if (!AsyncCloseable.class.isInstance(writeStream)) {
        return readFuture;
    }

    return closeWriteStream((AsyncCloseable<Void>) writeStream, readFuture);
}

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();//from  w w w  .  j  a  v a  2 s . c  o  m
    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;
    });
}