List of usage examples for io.netty.util.internal RecyclableArrayList add
@Override public boolean add(Object element)
From source file:code.google.nfs.rpc.netty.serialize.NettyProtocolDecoder.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { RecyclableArrayList out = RecyclableArrayList.newInstance(); try {//from ww w . j a v a2 s . c o m if (msg instanceof ByteBuf) { ByteBuf data = (ByteBuf) msg; if (cumulation == null) { cumulation = data; try { callDecode(ctx, cumulation, out); } finally { if (cumulation != null && !cumulation.isReadable()) { cumulation.release(); cumulation = null; } } } else { try { if (cumulation.writerIndex() > cumulation.maxCapacity() - data.readableBytes()) { ByteBuf oldCumulation = cumulation; cumulation = ctx.alloc().buffer(oldCumulation.readableBytes() + data.readableBytes()); cumulation.writeBytes(oldCumulation); oldCumulation.release(); } cumulation.writeBytes(data); callDecode(ctx, cumulation, out); } finally { if (cumulation != null) { if (!cumulation.isReadable()) { cumulation.release(); cumulation = null; } else { cumulation.discardSomeReadBytes(); } } data.release(); } } } else { out.add(msg); } } catch (DecoderException e) { throw e; } catch (Throwable t) { throw new DecoderException(t); } finally { if (out.isEmpty()) { decodeWasNull = true; } List<Object> results = new ArrayList<Object>(); for (Object result : out) { results.add(result); } ctx.fireChannelRead(results); out.recycle(); } }
From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPMessage.java
License:Apache License
/** * Convenience method for reading a {@link ZMTPMessage} from a {@link ByteBuf}. */// w w w . ja va 2 s. com public static ZMTPMessage read(final ByteBuf in, final ZMTPVersion version) throws ZMTPParsingException { final int mark = in.readerIndex(); final ZMTPWireFormat wireFormat = ZMTPWireFormats.wireFormat(version); final ZMTPWireFormat.Header header = wireFormat.header(); final RecyclableArrayList frames = RecyclableArrayList.newInstance(); while (true) { final boolean read = header.read(in); if (!read) { frames.recycle(); in.readerIndex(mark); return null; } if (in.readableBytes() < header.length()) { frames.recycle(); in.readerIndex(mark); return null; } if (header.length() > Integer.MAX_VALUE) { throw new ZMTPParsingException("frame is too large: " + header.length()); } final ByteBuf frame = in.readSlice((int) header.length()); frame.retain(); frames.add(frame); if (!header.more()) { @SuppressWarnings("unchecked") final ZMTPMessage message = ZMTPMessage.from((List<ByteBuf>) (Object) frames); frames.recycle(); return message; } } }