List of usage examples for io.netty.buffer ByteBuf retain
@Override public abstract ByteBuf retain();
From source file:org.elasticsearch.http.nio.PagedByteBufTests.java
License:Apache License
public void testReleasingPage() { AtomicInteger integer = new AtomicInteger(0); int pageCount = randomInt(10) + 1; ArrayList<InboundChannelBuffer.Page> pages = new ArrayList<>(); for (int i = 0; i < pageCount; ++i) { pages.add(new InboundChannelBuffer.Page(ByteBuffer.allocate(10), integer::incrementAndGet)); }//from ww w . ja v a 2 s. co m ByteBuf byteBuf = PagedByteBuf.byteBufFromPages(pages.toArray(new InboundChannelBuffer.Page[0])); assertEquals(0, integer.get()); byteBuf.retain(); byteBuf.release(); assertEquals(0, integer.get()); ByteBuf secondBuf = byteBuf.retainedSlice(); byteBuf.release(); assertEquals(0, integer.get()); secondBuf.release(); assertEquals(pageCount, integer.get()); }
From source file:org.gameoss.gridcast.p2p.serialization.ProtostuffEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception { if (msg instanceof ByteBuf) { // assume already serialized so just pass through ByteBuf buf = (ByteBuf) msg; out.add(buf.retain()); } else {// w w w . ja va2 s .co m // serialize ByteBuf buf = ctx.alloc().buffer(); serializeToByteBuf(registry, buf, msg); out.add(buf); } }
From source file:org.graylog2.gelfclient.encoder.GelfMessageChunkEncoder.java
License:Apache License
/** * {@inheritDoc}//from w ww .j a v a 2 s .co m */ @Override protected void encode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { if (buf.readableBytes() > MAX_MESSAGE_SIZE) { throw new EncoderException( "Message too big. " + buf.readableBytes() + " bytes (max " + MAX_MESSAGE_SIZE + ")"); } if (buf.readableBytes() <= MAX_CHUNK_SIZE) { // Need to retain() the buffer here to avoid releasing the buffer too early. out.add(buf.retain()); } else { final Chunker chunker = new Chunker(buf.readableBytes()); try { while (buf.readableBytes() > 0) { if (buf.readableBytes() >= MAX_CHUNK_SIZE) { out.add(chunker.nextChunk(buf.readSlice(MAX_CHUNK_SIZE))); } else { out.add(chunker.nextChunk(buf.readSlice(buf.readableBytes()))); } } } catch (Exception e) { LOG.error("Chunk encoder error", e); buf.release(); } } }
From source file:org.graylog2.gelfclient.encoder.GelfMessageUdpEncoder.java
License:Apache License
/** * {@inheritDoc}/*from w w w . jav a 2 s.c om*/ */ @Override protected void encode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { // Need to retain() the buffer here to avoid a io.netty.util.IllegalReferenceCountException. out.add(new DatagramPacket(buf.retain(), remoteAddress)); }
From source file:org.hornetq.amqp.test.invm.ProtonINVMSPI.java
License:Apache License
@Override public void output(final ByteBuf bytes, final ChannelFutureListener futureCompletion) { if (DebugInfo.debug) { ByteUtil.debugFrame("InVM->", bytes); }//from w w w . j av a 2s. c o m bytes.retain(); mainExecutor.execute(new Runnable() { public void run() { try { if (DebugInfo.debug) { ByteUtil.debugFrame("InVMDone->", bytes); } serverConnection.inputBuffer(bytes); try { futureCompletion.operationComplete(null); } catch (Exception e) { e.printStackTrace(); } } finally { bytes.release(); } } }); }
From source file:org.kaazing.messaging.driver.transport.netty.tcp.NettySendingTransport.java
License:Apache License
private ChannelFuture writeAndFlush(DriverMessage driverMessage) { //TODO(JAF): Figure out why multiple sends are getting condensed down to a single send ByteBuf nettyMessage = tlNettyMessage.get(); nettyMessage.retain(); //TODO(JAF): Avoid making a new byte array with each send byte[] bytesToSend = new byte[driverMessage.getBufferLength()]; driverMessage.getBuffer().getBytes(driverMessage.getBufferOffset(), bytesToSend); nettyMessage.setBytes(0, bytesToSend); nettyMessage.writerIndex(bytesToSend.length); return sendingChannel.writeAndFlush(nettyMessage); }
From source file:org.kuali.test.proxyserver.TestProxyServer.java
License:Educational Community License
/** * * @param content//w ww . j a v a 2 s. c o m * @return */ public static byte[] getHttpPostContent(ByteBuf content) { byte[] retval = null; if (content.isReadable()) { content.retain(); ByteBuffer nioBuffer = content.nioBuffer(); retval = new byte[nioBuffer.remaining()]; nioBuffer.get(retval); content.release(); } return retval; }
From source file:org.lanternpowered.server.network.pipeline.MessageCompressionHandler.java
License:MIT License
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { ByteBuf prefixBuf = ctx.alloc().buffer(5); ByteBuf contentsBuf;/*from w ww . j av a2 s. c o m*/ if (msg.readableBytes() >= this.compressionThreshold) { // Message should be compressed int index = msg.readerIndex(); int length = msg.readableBytes(); byte[] sourceData = new byte[length]; msg.readBytes(sourceData); this.deflater.setInput(sourceData); this.deflater.finish(); byte[] compressedData = new byte[length]; int compressedLength = this.deflater.deflate(compressedData); this.deflater.reset(); if (compressedLength == 0) { // Compression failed in some weird way throw new EncoderException("Failed to compress message of size " + length); } else if (compressedLength >= length) { // Compression increased the size. threshold is probably too low // Send as an uncompressed packet writeVarInt(prefixBuf, 0); msg.readerIndex(index); msg.retain(); contentsBuf = msg; } else { // All is well writeVarInt(prefixBuf, length); contentsBuf = Unpooled.wrappedBuffer(compressedData, 0, compressedLength); } } else { // Message should be sent through writeVarInt(prefixBuf, 0); msg.retain(); contentsBuf = msg; } out.add(Unpooled.wrappedBuffer(prefixBuf, contentsBuf)); }
From source file:org.lanternpowered.server.network.pipeline.MessageCompressionHandler.java
License:MIT License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { int index = msg.readerIndex(); int uncompressedSize = readVarInt(msg); if (uncompressedSize == 0) { // Message is uncompressed int length = msg.readableBytes(); if (length >= this.compressionThreshold) { // Invalid throw new DecoderException("Received uncompressed message of size " + length + " greater than threshold " + this.compressionThreshold); }//from w w w .j a v a 2 s . c om ByteBuf buf = ctx.alloc().buffer(length); msg.readBytes(buf, length); out.add(buf); } else { // Message is compressed byte[] sourceData = new byte[msg.readableBytes()]; msg.readBytes(sourceData); this.inflater.setInput(sourceData); byte[] destData = new byte[uncompressedSize]; int resultLength = this.inflater.inflate(destData); this.inflater.reset(); if (resultLength == 0) { // Might be a leftover from before compression was enabled (no compression header) // UncompressedSize is likely to be < threshold msg.readerIndex(index); msg.retain(); out.add(msg); } else if (resultLength != uncompressedSize) { throw new DecoderException("Received compressed message claiming to be of size " + uncompressedSize + " but actually " + resultLength); } else { out.add(Unpooled.wrappedBuffer(destData)); } } }
From source file:org.midonet.util.netty.WSFrameToBinaryDecoder.java
License:Apache License
@Override public void decode(ChannelHandlerContext ctx, BinaryWebSocketFrame frame, List<Object> out) { ByteBuf buf = frame.content(); out.add(buf.retain()); // TODO: check if frame reference counter should be decreased }