Example usage for io.netty.buffer CompositeByteBuf addComponents

List of usage examples for io.netty.buffer CompositeByteBuf addComponents

Introduction

In this page you can find the example usage for io.netty.buffer CompositeByteBuf addComponents.

Prototype

public CompositeByteBuf addComponents(Iterable<ByteBuf> buffers) 

Source Link

Document

Add the given ByteBuf s.

Usage

From source file:com.talent.nio.communicate.receive.DecodeRunnable.java

License:Open Source License

@Override
public void run() {
    while (getMsgQueue().size() > 0) {
        ByteBuf queuedatas = null;/*from w ww  .ja  v a  2  s  . c  om*/
        CompositeByteBuf datas = Unpooled.compositeBuffer();

        if (lastDatas != null) {
            channelContext.getStatVo().setCurrentOgnzTimestamp(SystemTimer.currentTimeMillis());
            lastDatas.readerIndex(0);
            datas.addComponents(lastDatas);
            lastDatas = null;
        }

        int count = 0;

        label_2: while ((queuedatas = getMsgQueue().poll()) != null) {
            queuedatas = queuedatas.order(channelContext.getByteOrder());

            if (DebugUtils.isNeedDebug(channelContext)) {
                // long xx = 999999999999999999L;
                log.error("queuedatas:" + ArrayUtils.toString(queuedatas));
            }
            datas.addComponents(queuedatas);
            channelContext.getStatVo().setCurrentOgnzTimestamp(SystemTimer.currentTimeMillis());
            count++;

            if (needLength != -1) // ????
            {
                if (datas.capacity() < needLength) // ??
                {
                    //                        log.error("??----capacity:{}, needLength:{}", datas.capacity(), needLength);
                    continue;
                } else {
                    //                        log.error("?----capacity:{}, needLength:{}", datas.capacity(), needLength);
                    break label_2;
                }

            } else
            // ???
            {
                if (count == 50) {
                    log.warn(
                            "???{}???{}",
                            count, getMsgQueue().size());
                    break label_2;
                }
            }
        }
        channelContext.getStatVo().setCurrentOgnzTimestamp(SystemTimer.currentTimeMillis());

        PacketWithMeta packetWithMeta = null;
        try {
            // ByteBuffer buffer = ByteBuffer.wrap(datas);
            datas.writerIndex(datas.capacity());
            datas.readerIndex(0);
            packetWithMeta = channelContext.getDecoder().decode(datas, channelContext);
            needLength = -1;
            if (packetWithMeta == null) { // ???
                lastDatas = datas;
                lastDatas.readerIndex(0);

                if (DebugUtils.isNeedDebug(channelContext)) {
                    log.error("???:{}", lastDatas);
                }
            } else if (packetWithMeta.getPackets() == null || packetWithMeta.getPackets().size() == 0) {
                // ???
                lastDatas = datas;
                lastDatas.readerIndex(0);
                needLength = packetWithMeta.getNeedLength();
                if (DebugUtils.isNeedDebug(channelContext)) {
                    log.error("????:{}", needLength);
                }
            } else {
                int len = packetWithMeta.getPacketLenght();
                // lastDatas = new byte[datas.capacity() - len];
                // System.arraycopy(datas, len, lastDatas, 0,
                // lastDatas.length);

                if (datas.capacity() - len > 0) {

                    lastDatas = datas.copy(len, datas.capacity() - len);
                    if (DebugUtils.isNeedDebug(channelContext)) {
                        log.error("??:{}, {}", datas.capacity() - len, lastDatas);
                    }
                } else {
                    lastDatas = null;
                    if (DebugUtils.isNeedDebug(channelContext)) {
                        log.error("??:{}", lastDatas);
                    }
                }
                processMsgAndStat(packetWithMeta.getPackets(), len, false);
            }

        } catch (DecodeException e) {
            log.error(e.getMessage(), e);
            channelContext.getErrorPackageHandler().handle(channelContext.getSocketChannel(), channelContext,
                    e.getMessage());
        }
    }

}

From source file:com.uber.tchannel.codecs.CodecUtils.java

License:Open Source License

public static ByteBuf writeArgs(ByteBufAllocator allocator, ByteBuf header, List<ByteBuf> args) {
    int writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - header.readableBytes();
    List<ByteBuf> bufs = new ArrayList<>(7);
    bufs.add(header);//  w  w  w .j  av a  2  s. c om

    while (!args.isEmpty()) {
        ByteBuf arg = args.get(0);
        int len = writeArg(allocator, arg, writableBytes, bufs);
        writableBytes -= len;
        if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) {
            break;
        }

        if (arg.readableBytes() == 0) {
            args.remove(0);
        }
    }

    CompositeByteBuf comp = allocator.compositeBuffer();
    comp.addComponents(bufs);
    comp.writerIndex(TFrame.MAX_FRAME_PAYLOAD_LENGTH - writableBytes);

    return comp;
}