Example usage for io.vertx.core.streams WriteStream drainHandler

List of usage examples for io.vertx.core.streams WriteStream drainHandler

Introduction

In this page you can find the example usage for io.vertx.core.streams WriteStream drainHandler.

Prototype

@Fluent
WriteStream<T> drainHandler(@Nullable Handler<Void> handler);

Source Link

Document

Set a drain handler on the stream.

Usage

From source file:org.entcore.common.storage.impl.GridfsStorage.java

License:Open Source License

private static void gridfsReadChunkFile(final String id, final EventBus eb, final String gridfsAddress,
        final WriteStream writeStream, final Handler<Chunk> handler) {
    JsonObject find = new JsonObject();
    find.put("action", "countChunks");
    find.put("files_id", id);
    byte[] header = null;
    try {/*from w  ww.  jav a 2  s .c o  m*/
        header = find.toString().getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage(), e);
        handler.handle(null);
    }
    if (header != null) {
        Buffer buf = Buffer.buffer(header);
        buf.appendInt(header.length);
        eb.send(gridfsAddress, buf, new Handler<AsyncResult<Message<Object>>>() {
            @Override
            public void handle(AsyncResult<Message<Object>> res) {
                if (res.succeeded() && res.result().body() instanceof Long) {
                    Long number = (Long) res.result().body();
                    if (number == null || number == 0l) {
                        handler.handle(null);
                    } else {
                        final Handler[] handlers = new Handler[number.intValue()];
                        handlers[handlers.length - 1] = new Handler<Chunk>() {
                            @Override
                            public void handle(Chunk chunk) {
                                handler.handle(chunk);
                                handler.handle(new Chunk(-1, null));
                            }
                        };
                        for (int i = number.intValue() - 2; i >= 0; i--) {
                            final int j = i;
                            handlers[i] = new Handler<Chunk>() {
                                @Override
                                public void handle(final Chunk chunk) {
                                    if (writeStream != null && writeStream.writeQueueFull()) {
                                        writeStream.drainHandler(new Handler<Void>() {
                                            @Override
                                            public void handle(Void event) {
                                                log.debug("in drain handler");
                                                writeStream.drainHandler(null);
                                                handler.handle(chunk);
                                                getChunk(id, j + 1, eb, gridfsAddress, new Handler<Chunk>() {
                                                    @Override
                                                    public void handle(Chunk res) {
                                                        handlers[j + 1].handle(res);
                                                    }
                                                });
                                            }
                                        });
                                    } else {
                                        handler.handle(chunk);
                                        getChunk(id, j + 1, eb, gridfsAddress, new Handler<Chunk>() {
                                            @Override
                                            public void handle(Chunk res) {
                                                handlers[j + 1].handle(res);
                                            }
                                        });
                                    }
                                }
                            };
                        }
                        getChunk(id, 0, eb, gridfsAddress, handlers[0]);
                    }
                } else {
                    handler.handle(null);
                }
            }
        });
    }
}

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

License:Apache License

public static Observable<Void> append(Buffer buffer, final WriteStream<Buffer> ws) {
    return Observable.defer(() -> {

        ObservableFuture<Void> drainHandler = RxHelper.observableFuture();
        ws.exceptionHandler(drainHandler::fail);
        if (ws.writeQueueFull()) {
            ws.drainHandler(drainHandler::complete);
        } else {/*from  w ww .j a  v  a2  s. c o m*/
            drainHandler.complete(null);
        }

        return drainHandler.flatMap(aVoid -> {
            ObservableFuture<Void> writeHandler = RxHelper.observableFuture();
            ws.exceptionHandler(writeHandler::fail);
            ws.write(buffer);
            if (ws.writeQueueFull()) {
                ws.drainHandler(writeHandler::complete);
            } else {
                writeHandler.complete(null);
            }
            return writeHandler;
        });
    });
}