Example usage for io.netty.buffer CompositeByteBuf capacity

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

Introduction

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

Prototype

@Override
    public int capacity() 

Source Link

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 va2  s .com*/
        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:eu.stratosphere.runtime.io.network.netty.InboundEnvelopeDecoderTest.java

License:Apache License

private static ByteBuf encode(EmbeddedChannel ch, Envelope... envelopes) {
    for (Envelope env : envelopes) {
        ch.writeOutbound(env);/*  ww  w .j av  a 2  s.  c om*/

        if (env.getBuffer() != null) {
            verify(env.getBuffer(), times(1)).recycleBuffer();
        }
    }

    CompositeByteBuf encodedEnvelopes = new CompositeByteBuf(ByteBufAllocator.DEFAULT, false, envelopes.length);

    ByteBuf buf;
    while ((buf = (ByteBuf) ch.readOutbound()) != null) {
        encodedEnvelopes.addComponent(buf);
    }

    return encodedEnvelopes.writerIndex(encodedEnvelopes.capacity());
}

From source file:org.cloudfoundry.reactor.util.MultipartHttpOutbound.java

License:Apache License

public Mono<Void> done() {
    AsciiString boundary = generateMultipartBoundary();
    ByteBufAllocator allocator = this.outbound.delegate().alloc();

    CompositeByteBuf bodyBuf = allocator.compositeBuffer();
    this.partConsumers
            .forEach(partConsumer -> bodyBuf.addComponent(getPart(allocator, boundary, partConsumer)));
    bodyBuf.addComponent(getCloseDelimiter(allocator, boundary));

    return this.outbound.removeTransferEncodingChunked()
            .addHeader(CONTENT_TYPE, MULTIPART_FORM_DATA.concat(BOUNDARY_PREAMBLE).concat(boundary))
            .addHeader(CONTENT_LENGTH, String.valueOf(bodyBuf.capacity()))
            .sendOne(bodyBuf.writerIndex(bodyBuf.capacity()));
}

From source file:org.cloudfoundry.reactor.util.MultipartHttpOutbound.java

License:Apache License

private static ByteBuf getPart(ByteBufAllocator allocator, AsciiString boundary,
        Consumer<PartHttpOutbound> partConsumer) {
    PartHttpOutbound part = new PartHttpOutbound();
    partConsumer.accept(part);//from  w w w. j  av a  2 s. c  o m

    CompositeByteBuf body = allocator.compositeBuffer();
    body.addComponent(getDelimiter(allocator, boundary));
    body.addComponent(getHeaders(allocator, part.getHeaders()));
    body.addComponent(getData(allocator, part.getInputStream()));

    return body.writerIndex(body.capacity());
}

From source file:org.dcache.xrootd.protocol.messages.ReadVResponse.java

License:Open Source License

@Override
public void writeTo(ChannelHandlerContext ctx, ChannelPromise promise) {
    checkState(refCnt() > 0);//from   w w  w.j  a  va  2s.  c o m

    CompositeByteBuf buffer = ctx.alloc().compositeBuffer(2 * length + 1);

    ByteBuf header = ctx.alloc().buffer(8);
    header.writeShort(request.getStreamId());
    header.writeShort(stat);
    header.writeInt(getDataLength());
    buffer.addComponent(header);

    for (int i = 0; i < length; i++) {
        header = ctx.alloc().buffer(READ_LIST_HEADER_SIZE);
        header.writeInt(requests[index + i].getFileHandle());
        header.writeInt(data[index + i].readableBytes());
        header.writeLong(requests[index + i].getOffset());
        buffer.addComponent(header);
        buffer.addComponent(data[index + i].retain());
    }

    buffer.writerIndex(buffer.capacity());
    ctx.write(buffer, promise);

    release();
}

From source file:org.wso2.carbon.gateway.internal.mediation.camel.CarbonMessageTypeConverter.java

License:Open Source License

@SuppressWarnings("unchecked")

public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
    if (value instanceof CarbonMessage) {
        //Retrieving the Pipe from the carbon message
        Pipe pipe = ((CarbonMessage) value).getPipe();
        //Input stream used for building the desired message
        ByteBufInputStream byteBufInputStream = null;
        //Create a composite buffer from content chunks in the pipe
        CompositeByteBuf contentBuf = aggregateChunks(pipe);
        //Check whether we have any content to be processed
        if (contentBuf.capacity() != 0) {
            try {
                if (type.isAssignableFrom(Document.class)) {
                    //Convert the input stream into xml dom element
                    return (T) toDocument(contentBuf, exchange);
                } else if (type.isAssignableFrom(DOMSource.class)) {
                    return (T) toDOMSource(contentBuf, exchange);
                } else if (type.isAssignableFrom(SAXSource.class)) {
                    return (T) toSAXSource(contentBuf, exchange);
                } else if (type.isAssignableFrom(StAXSource.class)) {
                    return (T) toStAXSource(contentBuf, exchange);
                } else if (type.isAssignableFrom(StreamSource.class)) {
                    return (T) toStreamSource(contentBuf, exchange);
                } else if (type.isAssignableFrom(InputStream.class)) {
                    return (T) toInputStream(contentBuf, exchange);
                } else if (type.isAssignableFrom(String.class)) {
                    return (T) toString(contentBuf, exchange);
                }/*from   w  w  w .j a  v a 2s. c o m*/
            } catch (UnsupportedEncodingException e) {
                log.error("Error occurred during type conversion", e);
            } finally {
                //Release the buffer if all the content has been consumed
                if (contentBuf.readerIndex() == contentBuf.writerIndex()) {
                    contentBuf.release();
                }
            }
        }

    }
    return null;
}