List of usage examples for io.netty.buffer Unpooled EMPTY_BUFFER
ByteBuf EMPTY_BUFFER
To view the source code for io.netty.buffer Unpooled EMPTY_BUFFER.
Click Source Link
From source file:io.advantageous.conekt.http.impl.ConektHttpHandler.java
License:Open Source License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (msg instanceof WebSocketFrameInternal) { WebSocketFrameInternal frame = (WebSocketFrameInternal) msg; ByteBuf buf = frame.getBinaryData(); if (buf != Unpooled.EMPTY_BUFFER) { buf = safeBuffer(buf, ctx.alloc()); }/* w ww. ja va2 s . c om*/ switch (frame.type()) { case BINARY: msg = new BinaryWebSocketFrame(frame.isFinal(), 0, buf); break; case TEXT: msg = new TextWebSocketFrame(frame.isFinal(), 0, buf); break; case CLOSE: msg = new CloseWebSocketFrame(true, 0, buf); break; case CONTINUATION: msg = new ContinuationWebSocketFrame(frame.isFinal(), 0, buf); break; case PONG: msg = new PongWebSocketFrame(buf); break; case PING: msg = new PingWebSocketFrame(buf); break; default: throw new IllegalStateException("Unsupported websocket msg " + msg); } } ctx.write(msg, promise); }
From source file:io.advantageous.conekt.http.impl.HttpClientRequestImpl.java
License:Open Source License
@Override public void end() { synchronized (getLock()) { checkComplete(); checkResponseHandler(); write(Unpooled.EMPTY_BUFFER, true); } }
From source file:io.advantageous.conekt.http.impl.HttpClientRequestImpl.java
License:Open Source License
private void connected(ClientConnection conn) { conn.setCurrentRequest(this); this.conn = conn; this.metric = client.httpClientMetrics().requestBegin(conn.metric(), conn.localAddress(), conn.remoteAddress(), this); // If anything was written or the request ended before we got the connection, then // we need to write it now if (pendingMaxSize != -1) { conn.doSetWriteQueueMaxSize(pendingMaxSize); }/*w ww .j a v a2 s. c om*/ if (pendingChunks != null) { ByteBuf pending = pendingChunks; pendingChunks = null; if (completed) { // we also need to write the head so optimize this and write all out in once writeHeadWithContent(pending, true); conn.reportBytesWritten(written); if (respHandler != null) { conn.endRequest(); } } else { writeHeadWithContent(pending, false); } } else { if (completed) { // we also need to write the head so optimize this and write all out in once writeHeadWithContent(Unpooled.EMPTY_BUFFER, true); conn.reportBytesWritten(written); if (respHandler != null) { conn.endRequest(); } } else { if (writeHead) { writeHead(); } } } }
From source file:io.advantageous.conekt.http.impl.HttpServerResponseImpl.java
License:Open Source License
@Override public MultiMap trailers() { if (trailers == null) { if (trailing == null) { trailing = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, false); }//from ww w . j av a2s .c om trailers = new HeadersAdaptor(trailing.trailingHeaders()); } return trailers; }
From source file:io.advantageous.conekt.http.impl.HttpServerResponseImpl.java
License:Open Source License
@Override public void end() { synchronized (conn) { end0(Unpooled.EMPTY_BUFFER); } }
From source file:io.advantageous.conekt.http.impl.ws.WebSocketFrameImpl.java
License:Open Source License
/** * Creates a new empty text frame. */ public WebSocketFrameImpl() { this(null, Unpooled.EMPTY_BUFFER, true); }
From source file:io.advantageous.conekt.http.impl.ws.WebSocketFrameImpl.java
License:Open Source License
/** * Creates a new empty text frame. */ public WebSocketFrameImpl(FrameType frameType) { this(frameType, Unpooled.EMPTY_BUFFER, true); }
From source file:io.advantageous.conekt.net.impl.ConektHandler.java
License:Open Source License
protected static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) { if (buf == Unpooled.EMPTY_BUFFER) { return buf; }//from www . j av a2 s . c o m if (buf.isDirect() || buf instanceof CompositeByteBuf) { try { if (buf.isReadable()) { ByteBuf buffer = allocator.heapBuffer(buf.readableBytes()); buffer.writeBytes(buf); return buffer; } else { return Unpooled.EMPTY_BUFFER; } } finally { buf.release(); } } return buf; }
From source file:io.advantageous.conekt.test.core.EventLoopGroupTest.java
License:Open Source License
@Test public void testNettyServerUsesContextEventLoop() throws Exception { ContextInternal context = (ContextInternal) conekt.getOrCreateContext(); AtomicReference<Thread> contextThread = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); context.runOnContext(v -> {// w w w . ja v a 2 s . c o m contextThread.set(Thread.currentThread()); latch.countDown(); }); awaitLatch(latch); ServerBootstrap bs = new ServerBootstrap(); bs.group(context.nettyEventLoop()); bs.channel(NioServerSocketChannel.class); 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(() -> { assertSame(contextThread.get(), Thread.currentThread()); assertSame(context, Conekt.currentContext()); ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { assertSame(contextThread.get(), Thread.currentThread()); context.executeFromIO(() -> { assertSame(contextThread.get(), Thread.currentThread()); assertSame(context, Conekt.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(() -> { assertSame(contextThread.get(), Thread.currentThread()); assertSame(context, Conekt.currentContext()); }); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { assertSame(contextThread.get(), Thread.currentThread()); context.executeFromIO(() -> { assertSame(contextThread.get(), Thread.currentThread()); assertSame(context, Conekt.currentContext()); ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); }); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { assertSame(contextThread.get(), Thread.currentThread()); context.executeFromIO(() -> { assertSame(contextThread.get(), Thread.currentThread()); assertSame(context, Conekt.currentContext()); testComplete(); }); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { fail(cause.getMessage()); } }); }); } }); bs.bind("localhost", 1234).sync(); conekt.createNetClient(new NetClientOptions()).connect(1234, "localhost", ar -> { assertTrue(ar.succeeded()); NetSocket so = ar.result(); so.write(Buffer.buffer("hello")); }); await(); }
From source file:io.airlift.drift.transport.netty.codec.HeaderTransport.java
License:Apache License
private static ByteBuf encodeHeaders(Map<String, String> headers) { if (headers.isEmpty()) { return Unpooled.EMPTY_BUFFER; }//from ww w.j a v a2 s . c o m // 1 bytes for header type, 5 for header count vint, and 5 for each header key and value length vint int estimatedSize = 1 + 5 + (headers.size() * 10); for (Entry<String, String> entry : headers.entrySet()) { // assume the key and value are ASCII estimatedSize += entry.getKey().length() + entry.getValue().length(); } ByteBuf headersBuffer = Unpooled.buffer(estimatedSize); // non persistent header headersBuffer.writeByte(0x01); writeVint(headersBuffer, headers.size()); for (Entry<String, String> entry : headers.entrySet()) { writeString(headersBuffer, entry.getKey()); writeString(headersBuffer, entry.getValue()); } return headersBuffer; }