Example usage for io.netty.buffer Unpooled EMPTY_BUFFER

List of usage examples for io.netty.buffer Unpooled EMPTY_BUFFER

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled EMPTY_BUFFER.

Prototype

ByteBuf EMPTY_BUFFER

To view the source code for io.netty.buffer Unpooled EMPTY_BUFFER.

Click Source Link

Document

A buffer whose capacity is 0 .

Usage

From source file:reactor.io.net.impl.netty.http.NettyHttpWSClientHandler.java

License:Open Source License

@Override
protected void doOnTerminate(ChannelHandlerContext ctx, ChannelFuture last, final ChannelPromise promise) {
    if (ctx.channel().isOpen()) {
        ChannelFutureListener listener = new ChannelFutureListener() {
            @Override//from  w  ww  . j a v a2  s  . com
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    promise.trySuccess();
                } else {
                    promise.tryFailure(future.cause());
                }
            }
        };

        if (last != null) {
            ctx.flush();
            last.addListener(listener);
        } else {
            ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(listener);
        }
    } else {
        promise.trySuccess();
    }
}

From source file:reactor.io.net.impl.netty.NettyChannelHandlerBridge.java

License:Apache License

@SuppressWarnings("unchecked")
protected final void doRead(ChannelHandlerContext ctx, Object msg) {
    try {/*from  ww w  . java 2s  .c o m*/
        if (null == channelSubscription || msg == Unpooled.EMPTY_BUFFER) {
            ReferenceCountUtil.release(msg);
            return;
        }

        if (channelStream.getDecoder() == Spec.NOOP_DECODER
                || !ByteBuf.class.isAssignableFrom(msg.getClass())) {
            channelSubscription.onNext((IN) msg);
            return;
        } else if (channelStream.getDecoder() == null) {
            try {
                channelSubscription.onNext((IN) new Buffer(((ByteBuf) msg).nioBuffer()));
            } finally {
                ReferenceCountUtil.release(msg);
            }
            return;
        }

        ByteBuf data = (ByteBuf) msg;
        if (remainder == null) {
            try {
                passToConnection(data);
            } finally {
                if (data.isReadable()) {
                    remainder = data;
                } else {
                    data.release();
                }
            }
            return;
        }

        if (!bufferHasSufficientCapacity(remainder, data)) {
            ByteBuf combined = createCombinedBuffer(remainder, data, ctx);
            remainder.release();
            remainder = combined;
        } else {
            remainder.writeBytes(data);
        }
        data.release();

        try {
            passToConnection(remainder);
        } finally {
            if (remainder.isReadable()) {
                remainder.discardSomeReadBytes();
            } else {
                remainder.release();
                remainder = null;
            }
        }
    } catch (Throwable t) {
        if (channelSubscription != null) {
            channelSubscription.onError(t);
        } else if (Environment.alive()) {
            Environment.get().routeError(t);
        }
    }

}

From source file:reactor.io.net.impl.netty.NettyChannelHandlerBridge.java

License:Apache License

protected ChannelFuture doOnWrite(Object data, ChannelHandlerContext ctx) {
    if (data.getClass().equals(Buffer.class)) {
        return ctx.channel().write(convertBufferToByteBuff(ctx, (Buffer) data));
    } else if (Unpooled.EMPTY_BUFFER != data) {
        return ctx.channel().write(data);
    }/*from   w w  w  . ja  va2  s. c o m*/
    return null;
}

From source file:reactor.io.net.impl.netty.NettyChannelHandlerBridge.java

License:Apache License

protected void doOnTerminate(ChannelHandlerContext ctx, ChannelFuture last, final ChannelPromise promise) {
    if (ctx.channel().isOpen()) {
        ChannelFutureListener listener = new ChannelFutureListener() {
            @Override/* ww  w.  j a  v a  2 s.  co  m*/
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    promise.trySuccess();
                } else {
                    promise.tryFailure(future.cause());
                }
            }
        };

        if (last != null) {
            ctx.flush();
            last.addListener(listener);
        } else {
            ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(listener);
        }
    } else {
        promise.trySuccess();
    }
}

From source file:reactor.ipc.netty.channel.ByteBufHolderHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof ByteBufHolder) {
        ByteBuf bb = ((ByteBufHolder) msg).content();
        if (bb == Unpooled.EMPTY_BUFFER || bb instanceof EmptyByteBuf) {
            ctx.fireChannelRead(msg);/*from w  ww.j av a 2s  . c o m*/
        } else {
            ctx.fireChannelRead(bb.retain());
        }
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:reactor.ipc.netty.channel.ChannelOperationsHandler.java

License:Open Source License

@Override
final public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg == null || msg == Unpooled.EMPTY_BUFFER || msg instanceof EmptyByteBuf) {
        return;/*w w  w . ja v a 2 s  .  c om*/
    }
    try {
        ChannelOperations<?, ?> ops = inbound();
        if (ops != null) {
            inbound().onInboundNext(ctx, msg);
        } else if (log.isDebugEnabled()) {
            log.debug("No ChannelOperation attached. Dropping: {}", msg);
        }
    } catch (Throwable err) {
        Exceptions.throwIfFatal(err);
        exceptionCaught(ctx, err);
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:reactor.ipc.netty.channel.NettyChannelHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg == null) {
        return;// w  w w.  java  2s.c om
    }
    try {
        if (msg == Unpooled.EMPTY_BUFFER || msg instanceof EmptyByteBuf) {
            return;
        }
        operations(ctx).onInboundNext(msg);
        ctx.fireChannelRead(msg);
    } catch (Throwable err) {
        Exceptions.throwIfFatal(err);
        operations(ctx).onChannelError(err);
    }
}

From source file:reactor.ipc.netty.channel.NettyOperations.java

License:Open Source License

/**
 * Write an individual packet (to be encoded further if the pipeline permits).
 *
 * @param data the payload to write on the {@link Channel}
 * @param ctx the actual {@link ChannelHandlerContext}
 *
 * @return the {@link ChannelFuture} of the successful/not payload write
 *///from ww w  . j  a  v  a2s  .  co m
protected ChannelFuture doOnWrite(Object data, ChannelHandlerContext ctx) {
    if (Unpooled.EMPTY_BUFFER != data) {
        return ctx.channel().write(data);
    }
    return null;
}

From source file:reactor.ipc.netty.http.client.HttpClientRequest.java

License:Open Source License

/**
 * Send headers and empty content thus delimiting a full empty body http request
 *
 * @return a {@link Mono} successful on committed response
 *
 * @see #send(Publisher)/*from   ww  w . ja  v a2 s.  c  o m*/
 */
default Mono<Void> send() {
    return sendObject(Unpooled.EMPTY_BUFFER).then();
}

From source file:reactor.ipc.netty.http.client.HttpClientTest.java

License:Open Source License

@Test
public void serverInfiniteClientClose() throws Exception {

    CountDownLatch latch = new CountDownLatch(1);
    NettyContext c = HttpServer.create(0).newHandler((req, resp) -> {
        req.context().onClose(latch::countDown);

        return Flux.interval(Duration.ofSeconds(1)).flatMap(d -> {
            req.context().channel().config().setAutoRead(true);

            return resp.sendObject(Unpooled.EMPTY_BUFFER).then()
                    .doOnSuccess(x -> req.context().channel().config().setAutoRead(false));
        });/*from  w ww.ja v a2  s.com*/
    }).block(Duration.ofSeconds(30));

    Mono<HttpClientResponse> remote = HttpClient.create(c.address().getPort()).get("/");

    HttpClientResponse r = remote.block();
    r.dispose();
    while (r.channel().isActive()) {
    }
    latch.await();
    c.dispose();
}