Example usage for io.netty.util.internal RecyclableArrayList newInstance

List of usage examples for io.netty.util.internal RecyclableArrayList newInstance

Introduction

In this page you can find the example usage for io.netty.util.internal RecyclableArrayList newInstance.

Prototype

public static RecyclableArrayList newInstance() 

Source Link

Document

Create a new empty RecyclableArrayList instance

Usage

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 {//www  . j a  va2s. co  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:code.google.nfs.rpc.netty.serialize.NettyProtocolDecoder.java

License:Apache License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    RecyclableArrayList out = RecyclableArrayList.newInstance();
    try {//w  w  w  . j ava2  s  . com
        if (cumulation != null) {
            callDecode(ctx, cumulation, out);
            decodeLast(ctx, cumulation, out);
        } else {
            decodeLast(ctx, Unpooled.EMPTY_BUFFER, out);
        }
    } catch (DecoderException e) {
        throw e;
    } catch (Exception e) {
        throw new DecoderException(e);
    } finally {
        if (cumulation != null) {
            cumulation.release();
            cumulation = null;
        }

        for (int i = 0; i < out.size(); i++) {
            ctx.fireChannelRead(out.get(i));
        }
        ctx.fireChannelInactive();
    }
}

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  . j  ava2 s.  co m
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;
        }
    }
}

From source file:divconq.net.ByteToMessageDecoder.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof ByteBuf) {
        RecyclableArrayList out = RecyclableArrayList.newInstance();

        try {// w w w .  j  a v a 2 s.co  m
            ByteBuf data = (ByteBuf) msg;
            this.first = this.cumulation == null;

            if (this.first) {
                this.cumulation = data;
            } else {
                if (this.cumulation.writerIndex() > this.cumulation.maxCapacity() - data.readableBytes()
                        || this.cumulation.refCnt() > 1) {
                    // Expand cumulation (by replace it) when either there is not more room in the buffer
                    // or if the refCnt is greater then 1 which may happen when the user use slice().retain() or
                    // duplicate().retain().
                    //
                    // See:
                    // - https://github.com/netty/netty/issues/2327
                    // - https://github.com/netty/netty/issues/1764
                    this.expandCumulation(ctx, data.readableBytes());
                }

                this.cumulation.writeBytes(data);
                data.release();
            }

            this.callDecode(ctx, this.cumulation, out);
        } catch (DecoderException e) {
            throw e;
        } catch (Throwable t) {
            throw new DecoderException(t);
        } finally {
            if (this.cumulation != null && !this.cumulation.isReadable()) {
                this.cumulation.release();
                this.cumulation = null;
            }
            int size = out.size();
            this.decodeWasNull = size == 0;

            for (int i = 0; i < size; i++) {
                ctx.fireChannelRead(out.get(i));
            }
            out.recycle();
        }
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:divconq.net.ByteToMessageDecoder.java

License:Apache License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    RecyclableArrayList out = RecyclableArrayList.newInstance();
    try {//from w ww .  j av  a  2 s  .c  o m
        if (this.cumulation != null) {
            callDecode(ctx, this.cumulation, out);
            decodeLast(ctx, this.cumulation, out);
        } else {
            decodeLast(ctx, Unpooled.EMPTY_BUFFER, out);
        }
    } catch (DecoderException e) {
        throw e;
    } catch (Exception e) {
        throw new DecoderException(e);
    } finally {
        try {
            if (this.cumulation != null) {
                this.cumulation.release();
                this.cumulation = null;
            }
            int size = out.size();
            for (int i = 0; i < size; i++) {
                ctx.fireChannelRead(out.get(i));
            }
            if (size > 0) {
                // Something was read, call fireChannelReadComplete()
                ctx.fireChannelReadComplete();
            }
            ctx.fireChannelInactive();
        } finally {
            // recycle in all cases
            out.recycle();
        }
    }
}