List of usage examples for io.netty.buffer ByteBuf release
boolean release();
From source file:io.grpc.alts.internal.BufUnwrapperTest.java
License:Apache License
@Test public void readableNioBuffers_worksWithNormal() { ByteBuf buf = alloc.buffer(1).writeByte('a'); try (BufUnwrapper unwrapper = new BufUnwrapper()) { ByteBuffer[] internalBufs = unwrapper.readableNioBuffers(buf); Truth.assertThat(internalBufs).hasLength(1); assertEquals('a', internalBufs[0].get(0)); } finally {// w w w . j a v a 2s . c o m buf.release(); } }
From source file:io.grpc.alts.internal.BufUnwrapperTest.java
License:Apache License
@Test public void writableNioBuffers_indexesPreserved() { ByteBuf buf = alloc.buffer(1); int ridx = buf.readerIndex(); int widx = buf.writerIndex(); int cap = buf.capacity(); try (BufUnwrapper unwrapper = new BufUnwrapper()) { ByteBuffer[] internalBufs = unwrapper.writableNioBuffers(buf); Truth.assertThat(internalBufs).hasLength(1); internalBufs[0].put((byte) 'a'); assertEquals(ridx, buf.readerIndex()); assertEquals(widx, buf.writerIndex()); assertEquals(cap, buf.capacity()); } finally {//from w ww. j a va 2 s .com buf.release(); } }
From source file:io.grpc.alts.internal.BufUnwrapperTest.java
License:Apache License
@Test public void writableNioBuffers_worksWithNormal() { ByteBuf buf = alloc.buffer(1); try (BufUnwrapper unwrapper = new BufUnwrapper()) { ByteBuffer[] internalBufs = unwrapper.writableNioBuffers(buf); Truth.assertThat(internalBufs).hasLength(1); internalBufs[0].put((byte) 'a'); buf.writerIndex(1);/*from w w w .ja va2s . co m*/ assertEquals('a', buf.readByte()); } finally { buf.release(); } }
From source file:io.grpc.netty.NettyServerHandlerTest.java
License:Apache License
private ByteBuf emptyGrpcFrame(int streamId, boolean endStream) throws Exception { ByteBuf buf = NettyTestUtil.messageFrame(""); try {//from w w w .j a va 2 s . co m return dataFrame(streamId, endStream, buf); } finally { buf.release(); } }
From source file:io.grpc.netty.ProtocolNegotiatorsTest.java
License:Apache License
@Test public void httpProxy_completes() throws Exception { DefaultEventLoopGroup elg = new DefaultEventLoopGroup(1); // ProxyHandler is incompatible with EmbeddedChannel because when channelRegistered() is called // the channel is already active. LocalAddress proxy = new LocalAddress("httpProxy_completes"); SocketAddress host = InetSocketAddress.createUnresolved("specialHost", 314); ChannelInboundHandler mockHandler = mock(ChannelInboundHandler.class); Channel serverChannel = new ServerBootstrap().group(elg).channel(LocalServerChannel.class) .childHandler(mockHandler).bind(proxy).sync().channel(); ProtocolNegotiator nego = ProtocolNegotiators.httpProxy(proxy, null, null, ProtocolNegotiators.plaintext()); // normally NettyClientTransport will add WBAEH which kick start the ProtocolNegotiation, // mocking the behavior using KickStartHandler. ChannelHandler handler = new KickStartHandler( nego.newHandler(FakeGrpcHttp2ConnectionHandler.noopHandler())); Channel channel = new Bootstrap().group(elg).channel(LocalChannel.class).handler(handler).register().sync() .channel();/* www . ja v a2 s .co m*/ pipeline = channel.pipeline(); // Wait for initialization to complete channel.eventLoop().submit(NOOP_RUNNABLE).sync(); channel.connect(host).sync(); serverChannel.close(); ArgumentCaptor<ChannelHandlerContext> contextCaptor = ArgumentCaptor.forClass(ChannelHandlerContext.class); Mockito.verify(mockHandler).channelActive(contextCaptor.capture()); ChannelHandlerContext serverContext = contextCaptor.getValue(); final String golden = "isThisThingOn?"; ChannelFuture negotiationFuture = channel.writeAndFlush(bb(golden, channel)); // Wait for sending initial request to complete channel.eventLoop().submit(NOOP_RUNNABLE).sync(); ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class); Mockito.verify(mockHandler).channelRead(ArgumentMatchers.<ChannelHandlerContext>any(), objectCaptor.capture()); ByteBuf b = (ByteBuf) objectCaptor.getValue(); String request = b.toString(UTF_8); b.release(); assertTrue("No trailing newline: " + request, request.endsWith("\r\n\r\n")); assertTrue("No CONNECT: " + request, request.startsWith("CONNECT specialHost:314 ")); assertTrue("No host header: " + request, request.contains("host: specialHost:314")); assertFalse(negotiationFuture.isDone()); serverContext.writeAndFlush(bb("HTTP/1.1 200 OK\r\n\r\n", serverContext.channel())).sync(); negotiationFuture.sync(); channel.eventLoop().submit(NOOP_RUNNABLE).sync(); objectCaptor = ArgumentCaptor.forClass(Object.class); Mockito.verify(mockHandler, times(2)).channelRead(ArgumentMatchers.<ChannelHandlerContext>any(), objectCaptor.capture()); b = (ByteBuf) objectCaptor.getAllValues().get(1); // If we were using the real grpcHandler, this would have been the HTTP/2 preface String preface = b.toString(UTF_8); b.release(); assertEquals(golden, preface); channel.close(); }
From source file:io.grpc.netty.ProtocolNegotiatorsTest.java
License:Apache License
@Test public void httpProxy_500() throws Exception { DefaultEventLoopGroup elg = new DefaultEventLoopGroup(1); // ProxyHandler is incompatible with EmbeddedChannel because when channelRegistered() is called // the channel is already active. LocalAddress proxy = new LocalAddress("httpProxy_500"); SocketAddress host = InetSocketAddress.createUnresolved("specialHost", 314); ChannelInboundHandler mockHandler = mock(ChannelInboundHandler.class); Channel serverChannel = new ServerBootstrap().group(elg).channel(LocalServerChannel.class) .childHandler(mockHandler).bind(proxy).sync().channel(); ProtocolNegotiator nego = ProtocolNegotiators.httpProxy(proxy, null, null, ProtocolNegotiators.plaintext()); // normally NettyClientTransport will add WBAEH which kick start the ProtocolNegotiation, // mocking the behavior using KickStartHandler. ChannelHandler handler = new KickStartHandler( nego.newHandler(FakeGrpcHttp2ConnectionHandler.noopHandler())); Channel channel = new Bootstrap().group(elg).channel(LocalChannel.class).handler(handler).register().sync() .channel();/* w ww . jav a 2 s. c o m*/ pipeline = channel.pipeline(); // Wait for initialization to complete channel.eventLoop().submit(NOOP_RUNNABLE).sync(); channel.connect(host).sync(); serverChannel.close(); ArgumentCaptor<ChannelHandlerContext> contextCaptor = ArgumentCaptor.forClass(ChannelHandlerContext.class); Mockito.verify(mockHandler).channelActive(contextCaptor.capture()); ChannelHandlerContext serverContext = contextCaptor.getValue(); final String golden = "isThisThingOn?"; ChannelFuture negotiationFuture = channel.writeAndFlush(bb(golden, channel)); // Wait for sending initial request to complete channel.eventLoop().submit(NOOP_RUNNABLE).sync(); ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class); Mockito.verify(mockHandler).channelRead(any(ChannelHandlerContext.class), objectCaptor.capture()); ByteBuf request = (ByteBuf) objectCaptor.getValue(); request.release(); assertFalse(negotiationFuture.isDone()); String response = "HTTP/1.1 500 OMG\r\nContent-Length: 4\r\n\r\noops"; serverContext.writeAndFlush(bb(response, serverContext.channel())).sync(); thrown.expect(ProxyConnectException.class); try { negotiationFuture.sync(); } finally { channel.close(); } }
From source file:io.hekate.network.netty.NetworkProtocolCodec.java
License:Apache License
static ByteBuf preEncode(Object msg, Codec<Object> codec, ByteBufAllocator allocator) throws CodecException { ByteBuf buf = allocator.buffer(); try {/*from w w w .j a v a 2 s . c om*/ ByteBufDataWriter writer = new ByteBufDataWriter(buf); doEncode(msg, writer, codec); return buf; } catch (CodecException e) { buf.release(); throw e; } }
From source file:io.hydramq.disk.DiskSegment.java
License:Open Source License
public void write(Message message, MessageIOListener messageIOListener) throws HydraRuntimeException { try {/* w w w. ja v a 2 s. c o m*/ ByteBuf buffer = allocator.directBuffer(); buffer.writeInt(0); conversionContext.write(message, buffer); int messageSize = buffer.readableBytes() - 4; buffer.setInt(0, messageSize); boolean shouldFlush = flushStrategy.requiresFlush(buffer.readableBytes()); indexWriteBuffer.clear(); indexWriteBuffer.putInt((int) data.size()); indexWriteBuffer.putLong(Clock.systemUTC().millis()); indexWriteBuffer.flip(); ByteBuffer nioBuffer = buffer.nioBuffer(); while (nioBuffer.hasRemaining()) { data.write(nioBuffer); } while (indexWriteBuffer.hasRemaining()) { index.write(indexWriteBuffer); } buffer.release(); if (shouldFlush) { data.force(true); index.force(true); } size += 1; if (messageIOListener != null) { messageIOListener.onMessage(1, messageSize); } } catch (IOException ex) { throw new HydraRuntimeException("Error writing message to segment " + segmentDirectory.toString()); } }
From source file:io.hydramq.network.server.BaseProtocolHandler.java
License:Open Source License
@Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception { if (msg instanceof ByteBuf) { ByteBuf buffer = (ByteBuf) msg; Command command = getConversionContext().read((ByteBuf) msg); buffer.release(); onCommand(ctx, command);//from w w w. j a v a2 s . c o m } else if (msg instanceof Command) { onCommand(ctx, (Command) msg); } }
From source file:io.jsql.mysql.mysql.ErrorPacket.java
License:Open Source License
public byte[] writeToBytes2() { ByteBuf buffer = Unpooled.buffer(calcPacketSize() + 4); int size = calcPacketSize(); MBufferUtil.writeUB3(buffer, size);//ww w .j av a2 s . co m buffer.writeByte(packetId); buffer.writeByte(fieldCount); MBufferUtil.writeUB2(buffer, errno); buffer.writeByte(mark); buffer.writeBytes(sqlState); if (message != null) { buffer.writeBytes(message); } byte[] data = new byte[buffer.readableBytes()]; buffer.readBytes(data); buffer.release(); return data; }