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

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

Introduction

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

Prototype

boolean writeQueueFull();

Source Link

Document

This will return true if there are more bytes in the write queue than the value set using #setWriteQueueMaxSize

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  ww w .j  a v  a2 s  . c  om
        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 {/*w w w .  j  av a 2s  .co  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;
        });
    });
}