List of usage examples for io.netty.buffer ByteBuf release
boolean release();
From source file:com.shouxun.server.netty.tcp.EchoServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf buffer = (ByteBuf) msg; for (int i = 0; i < buffer.capacity(); i++) { byte b = buffer.getByte(i); sb.append((char) b); }/*from www. j ava 2 s.c o m*/ buffer.release(); }
From source file:com.spotify.ffwd.riemann.RiemannFrameDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return;// w w w . j ava 2s . c o m } final long length = in.getUnsignedInt(0); if (length > MAX_SIZE) { throw new CorruptedFrameException( String.format("frame size (%s) larger than max (%d)", length, MAX_SIZE)); } final int intLength = (int) length; if (in.readableBytes() < (4 + length)) { return; } in.skipBytes(4); final ByteBuf frame = in.readBytes(intLength); try { out.add(serializer.parse0(frame)); } finally { frame.release(); } }
From source file:com.spotify.ffwd.riemann.RiemannResponder.java
License:Apache License
private ByteBuf ack(boolean ok) throws IOException { final Proto.Msg m = Proto.Msg.newBuilder().setOk(ok).build(); final ByteBuf b = Unpooled.buffer(); try (final ByteBufOutputStream output = new ByteBufOutputStream(b)) { m.writeTo(output);//w w w.j a v a 2 s . c o m final ByteBuf frame = output.buffer(); final ByteBuf buffer = Unpooled.buffer(4 + frame.readableBytes()); buffer.writeInt(frame.readableBytes()); buffer.writeBytes(frame); return buffer; } finally { b.release(); } }
From source file:com.spotify.ffwd.riemann.RiemannSerialization.java
License:Apache License
public ByteBuf encodeAll0(Collection<Object> messages) throws IOException { final Proto.Msg.Builder builder = Proto.Msg.newBuilder(); for (final Object d : messages) { if (d instanceof Metric) { builder.addEvents(encodeMetric0((Metric) d)); } else if (d instanceof Event) { builder.addEvents(encodeEvent0((Event) d)); }/*from ww w.j a va 2s.com*/ } final Proto.Msg m = builder.build(); final ByteBuf work = Unpooled.buffer(); try (final ByteBufOutputStream output = new ByteBufOutputStream(work)) { m.writeTo(output); final ByteBuf result = Unpooled.buffer(); result.writeInt(work.writerIndex()); result.writeBytes(work); return result; } finally { work.release(); } }
From source file:com.spotify.netty.zmtp.Benchmark.java
License:Apache License
@Ignore("this is a benchmark") @Test/* w w w . j a v a 2 s. co m*/ public void benchmarkEncoding() throws ZMTPMessageParsingException { final ProgressMeter meter = new ProgressMeter("messages"); ZMTPMessage message = new ZMTPMessage( asList(ZMTPFrame.create("first identity frame"), ZMTPFrame.create("second identity frame")), asList(ZMTPFrame.create("datadatadatadatadatadatadatadatadatadata"), ZMTPFrame.create("datadatadatadatadatadatadatadatadatadata"), ZMTPFrame.create("datadatadatadatadatadatadatadatadatadata"), ZMTPFrame.create("datadatadatadatadatadatadatadatadatadata"))); final ZMTPMessageParser parser = new ZMTPMessageParser(true, 1024 * 1024, 1); long sum = 0; for (long i = 0; i < 1000000; i++) { for (long j = 0; j < 1000; j++) { final ByteBuf buffer = Unpooled.buffer(ZMTPUtils.messageSize(message, true, 1)); ZMTPUtils.writeMessage(message, buffer, true, 1); message = parser.parse(buffer).getMessage(); sum += buffer.readableBytes(); buffer.release(); } meter.inc(1000, 0); } System.out.println(sum); }
From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPMessage.java
License:Apache License
@Override protected void deallocate() { for (final ByteBuf frame : frames) { frame.release(); } }
From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPMessageDecoder.java
License:Apache License
@Override public void close() { for (final ByteBuf frame : frames) { frame.release(); }/* w w w. j av a 2s.c o m*/ frames.clear(); }
From source file:com.streamsets.pipeline.lib.parser.net.TestDelimitedLengthFieldBasedFrameDecoder.java
License:Apache License
private void writeStringAndAssert(EmbeddedChannel channel, String value, Charset charset, boolean randomlyPartition, boolean expectFrameTooLarge) { String frame = makeFrame(value, charset); try {/* w ww . ja v a 2 s . c o m*/ if (randomlyPartition) { for (List<Byte> chunk : getRandomByteSlices(frame.getBytes())) { channel.writeInbound(Unpooled.copiedBuffer(Bytes.toArray(chunk))); } } else { channel.writeInbound(Unpooled.copiedBuffer(frame, charset)); } } catch (TooLongFrameException e) { if (!expectFrameTooLarge) { Assert.fail("TooLongFrameException unexpectedly thrown"); } else { Assert.assertNull(channel.readInbound()); } } if (!expectFrameTooLarge) { ByteBuf in = (ByteBuf) channel.readInbound(); Assert.assertEquals(value, in.toString(charset)); in.release(); } }
From source file:com.tc.websocket.server.handler.WebSocketValidationHandler.java
License:Apache License
/** * Send http response./*from ww w . ja va2s. c om*/ * * @param ctx the ctx * @param req the req * @param res the res */ private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf); buf.release(); HttpUtil.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.tesora.dve.db.mysql.libmy.MyBinaryResultRow.java
License:Open Source License
@Override public void unmarshallMessage(ByteBuf cb) throws PEException { int expectedFieldCount = fieldConverters.size(); int expectedBitmapLength = MyNullBitmap.computeSize(expectedFieldCount, MyNullBitmap.BitmapType.RESULT_ROW); cb = cb.order(ByteOrder.LITTLE_ENDIAN); cb.skipBytes(1);//skip the bin row marker. byte[] nullBitmap = new byte[expectedBitmapLength]; cb.readBytes(nullBitmap);//w ww . j a v a 2 s . com MyNullBitmap resultBitmap = new MyNullBitmap(nullBitmap, expectedFieldCount, MyNullBitmap.BitmapType.RESULT_ROW); ByteBuf values = cb; for (int i = 0; i < expectedFieldCount; i++) { ByteBuf existing = fieldSlices.get(i); ByteBuf nextSlice = null; int startIndex = values.readerIndex(); if (!resultBitmap.getBit(i + 1)) { fieldConverters.get(i).readObject(values);//TODO: we throw out the unmarshalled value, we could cache it. int endingOffset = values.readerIndex(); nextSlice = values.slice(startIndex, endingOffset - startIndex); } if (existing != null) existing.release(); fieldSlices.set(i, nextSlice); } if (cb.readableBytes() > 0) { log.warn("Decoded binary row had {} leftover bytes, re-encoding may fail.", cb.readableBytes()); cb.skipBytes(cb.readableBytes());//consume rest of buffer. } }