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:io.scalecube.socketio.pipeline.JsonpPollingHandlerTest.java

License:Apache License

@Test
public void testChannelReadNonHttp() throws Exception {
    LastOutboundHandler lastOutboundHandler = new LastOutboundHandler();
    EmbeddedChannel channel = new EmbeddedChannel(lastOutboundHandler, jsonpPollingHandler);
    channel.writeInbound(Unpooled.EMPTY_BUFFER);
    Object object = channel.readInbound();
    Assert.assertTrue(object instanceof ByteBuf);
    Assert.assertEquals(Unpooled.EMPTY_BUFFER, object);
    channel.finish();/*from ww w.j  av  a  2  s.  com*/
}

From source file:io.scalecube.socketio.pipeline.WebSocketHandlerTest.java

License:Apache License

@Test
public void testChannelReadNonHttp() throws Exception {
    LastOutboundHandler lastOutboundHandler = new LastOutboundHandler();
    EmbeddedChannel channel = new EmbeddedChannel(lastOutboundHandler, webSocketHandler);
    channel.writeInbound(Unpooled.EMPTY_BUFFER);
    Object object = channel.readInbound();
    Assert.assertTrue(object instanceof ByteBuf);
    Assert.assertEquals(Unpooled.EMPTY_BUFFER, object);
    channel.finish();/*from   w  ww.  ja  va  2s  .c o m*/
}

From source file:io.scalecube.socketio.pipeline.XHRPollingHandlerTest.java

License:Apache License

@Test
public void testChannelReadNonHttp() throws Exception {
    LastOutboundHandler lastOutboundHandler = new LastOutboundHandler();
    EmbeddedChannel channel = new EmbeddedChannel(lastOutboundHandler, xhrPollingHandler);
    channel.writeInbound(Unpooled.EMPTY_BUFFER);
    Object object = channel.readInbound();
    Assert.assertTrue(object instanceof ByteBuf);
    Assert.assertEquals(Unpooled.EMPTY_BUFFER, object);
    channel.finish();/*w  w w  . j  av  a  2  s.  co m*/
}

From source file:io.termd.core.ssh.netty.NettyIoSession.java

License:Apache License

@Override
protected CloseFuture doCloseGracefully() {
    context.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE).addListener(fut -> {
        closeFuture.setClosed();/*from  w w w  .  j  a  v  a2  s . co m*/
    });
    return closeFuture;
}

From source file:io.termd.core.telnet.netty.NettyTelnetConnection.java

License:Apache License

@Override
public void close() {
    context.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}

From source file:io.urmia.md.model.ObjectResponse.java

License:Open Source License

public FullHttpResponse encode() {

    final HttpResponseStatus status;
    final ByteBuf content;

    if (this instanceof Failure) {
        status = ((Failure) this).type.status;
        content = Unpooled.copiedBuffer(json(), CharsetUtil.UTF_8);

    } else {//from   ww w  .ja  v a 2  s  . co  m
        final String json = this.json();

        if (isBlank(json)) {
            status = HttpResponseStatus.NO_CONTENT;
            content = Unpooled.EMPTY_BUFFER;
        } else {
            status = HttpResponseStatus.OK;
            content = Unpooled.copiedBuffer(json, CharsetUtil.UTF_8);
        }
    }

    FullHttpResponse httpResponse = new DefaultFullHttpResponse(HTTP_1_1, status, content);

    httpResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, this.contentType());
    //httpResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.json().length());
    httpResponse.headers().set("transfer-encoding", "chunked"); // this is a must for list response

    if (this.resultSetSize() > 0)
        httpResponse.headers().set(RESULT_SET_SIZE, this.resultSetSize());

    return httpResponse;

}

From source file:io.urmia.proxy.DirectWriteBackHttpProxyBackendHandler.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    log.info("backend active: {}", ctx);
    ctx.read();/*from   w  w  w .j a v  a  2 s.  c  o m*/
    ctx.write(Unpooled.EMPTY_BUFFER);
}

From source file:io.urmia.proxy.HttpProxyFrontendHandler.java

License:Open Source License

private static void closeOnFlush(Channel ch) {
    if (ch.isActive()) {
        log.info("closeOnFlush active ch: {}", ch);
        ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    } else {/*from   w  w w.  j  a  v a  2  s  .  co  m*/
        log.info("closeOnFlush inactive ch: {}", ch);
        ch.closeFuture();
    }

}

From source file:io.vertx.core.dns.impl.fix.DnsQueryContext.java

License:Apache License

DnsQueryContext(DnsNameResolver parent, InetSocketAddress nameServerAddr, DnsQuestion question,
        Iterable<DnsRecord> additional, Promise<AddressedEnvelope<DnsResponse, InetSocketAddress>> promise) {

    this.parent = checkNotNull(parent, "parent");
    this.nameServerAddr = checkNotNull(nameServerAddr, "nameServerAddr");
    this.question = checkNotNull(question, "question");
    this.additional = checkNotNull(additional, "additional");
    this.promise = checkNotNull(promise, "promise");
    recursionDesired = parent.isRecursionDesired();
    id = parent.queryContextManager.add(this);

    if (parent.isOptResourceEnabled()) {
        optResource = new DefaultDnsRawRecord(StringUtil.EMPTY_STRING, DnsRecordType.OPT,
                parent.maxPayloadSize(), 0, Unpooled.EMPTY_BUFFER);
    } else {// www . ja  v  a2s. c  o m
        optResource = null;
    }
}

From source file:io.vertx.core.EventLoopGroupTest.java

License:Open Source License

@Test
public void testNettyServerUsesContextEventLoop() throws Exception {
    ContextInternal context = (ContextInternal) vertx.getOrCreateContext();
    AtomicReference<Thread> contextThread = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    context.runOnContext(v -> {/*w w w .  java2s.c  o m*/
        contextThread.set(Thread.currentThread());
        latch.countDown();
    });
    awaitLatch(latch);
    ServerBootstrap bs = new ServerBootstrap();
    bs.group(context.nettyEventLoop());
    bs.channelFactory(((VertxInternal) vertx).transport().serverChannelFactory(false));
    bs.option(ChannelOption.SO_BACKLOG, 100);
    bs.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            assertSame(contextThread.get(), Thread.currentThread());
            context.executeFromIO(v -> {
                assertSame(contextThread.get(), Thread.currentThread());
                assertSame(context, Vertx.currentContext());
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                    @Override
                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                        assertSame(contextThread.get(), Thread.currentThread());
                        context.executeFromIO(v -> {
                            assertSame(contextThread.get(), Thread.currentThread());
                            assertSame(context, Vertx.currentContext());
                        });
                    }

                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        ByteBuf buf = (ByteBuf) msg;
                        assertEquals("hello", buf.toString(StandardCharsets.UTF_8));
                        assertSame(contextThread.get(), Thread.currentThread());
                        context.executeFromIO(v -> {
                            assertSame(contextThread.get(), Thread.currentThread());
                            assertSame(context, Vertx.currentContext());
                        });
                    }

                    @Override
                    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
                        assertSame(contextThread.get(), Thread.currentThread());
                        context.executeFromIO(v -> {
                            assertSame(contextThread.get(), Thread.currentThread());
                            assertSame(context, Vertx.currentContext());
                            ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
                        });
                    }

                    @Override
                    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                        assertSame(contextThread.get(), Thread.currentThread());
                        context.executeFromIO(v -> {
                            assertSame(contextThread.get(), Thread.currentThread());
                            assertSame(context, Vertx.currentContext());
                            testComplete();
                        });
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                        fail(cause.getMessage());
                    }
                });
            });
        }
    });
    ChannelFuture fut = bs.bind("localhost", 1234);
    try {
        fut.sync();
        vertx.createNetClient(new NetClientOptions()).connect(1234, "localhost", ar -> {
            assertTrue(ar.succeeded());
            NetSocket so = ar.result();
            so.write(Buffer.buffer("hello"));
        });
        await();
    } finally {
        fut.channel().close().sync();
    }
}