Example usage for io.netty.buffer Unpooled compositeBuffer

List of usage examples for io.netty.buffer Unpooled compositeBuffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled compositeBuffer.

Prototype

public static CompositeByteBuf compositeBuffer() 

Source Link

Document

Returns a new big-endian composite buffer with no components.

Usage

From source file:alluxio.network.protocol.databuffer.DataNettyBufferTest.java

License:Apache License

/**
 * Tests that an exception is thrown when two NIO buffers are used.
 *//*from w w w .  j  a v a  2  s .c  om*/
@Test
public void singleNioBufferCheckFailed() {
    mThrown.expect(IllegalArgumentException.class);
    mThrown.expectMessage("Number of nioBuffers of this bytebuf is 2 (1 expected).");
    releaseBuffer(); // not using the default ByteBuf given in Before()
    // creating a CompositeByteBuf with 2 NIO buffers
    mBuffer = Unpooled.compositeBuffer();
    ((CompositeByteBuf) mBuffer).addComponent(Unpooled.buffer(LENGTH));
    ((CompositeByteBuf) mBuffer).addComponent(Unpooled.buffer(LENGTH));
    new DataNettyBuffer(mBuffer, LENGTH);
}

From source file:com.linecorp.armeria.client.encoding.ZlibStreamDecoder.java

License:Apache License

private byte[] fetchDecoderOutput() {
    CompositeByteBuf decoded = Unpooled.compositeBuffer();
    for (;;) {//from w ww.  ja  v a2 s  . c om
        ByteBuf buf = decoder.readInbound();
        if (buf == null) {
            break;
        }
        if (!buf.isReadable()) {
            buf.release();
            continue;
        }
        decoded.addComponent(true, buf);
    }
    byte[] ret = ByteBufUtil.getBytes(decoded);
    decoded.release();
    return ret;
}

From source file:com.netflix.iep.http.ByteBufs.java

License:Apache License

/**
 * Create an aggregated byte array with all data from the ByteBufs in the input observable.
 *///  ww w  . j  a va 2  s  .  co  m
public static Observable.Transformer<ByteBuf, byte[]> aggrByteArray() {
    return input -> input.reduce(Unpooled.compositeBuffer(), append()).map(toByteArray());
}

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;//  ww w . j  av  a2 s  .co  m
        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.talent.nio.communicate.receive.DecodeRunnable.java

License:Open Source License

/**
 * @param args/*w  ww . j a v  a2s  .co m*/
 */
public static void main(String[] args) {
    byte[] bs1 = new byte[] { 1, 10, 11, 12 };
    byte[] bs2 = new byte[] { 2, 2, 2, 2 };
    byte[] bs3 = new byte[] { 3, 3, 3, 3 };
    byte[] bs4 = new byte[] { 4, 4, 4, 4 };
    byte[] bs5 = new byte[] { 5, 5, 5, 5 };
    byte[] bs6 = new byte[] { 6, 6, 6, 6 };

    ByteBuffer buffer1 = ByteBuffer.allocate(1024);
    buffer1.put(bs1);
    buffer1.flip();

    ByteBuf buf1 = Unpooled.copiedBuffer(buffer1);// .copiedBuffer(bs1);

    buffer1.put(bs3);

    ByteBuf buf2 = Unpooled.copiedBuffer(bs2);
    ByteBuf buf3 = Unpooled.copiedBuffer(bs3);
    ByteBuf buf4 = Unpooled.copiedBuffer(bs4);
    ByteBuf buf5 = Unpooled.copiedBuffer(bs5);
    ByteBuf buf6 = Unpooled.copiedBuffer(bs6);

    CompositeByteBuf cb = Unpooled.compositeBuffer();
    cb.addComponents(buf1, buf2, buf3);

    byte dd = cb.getByte(0);

    CompositeByteBuf cb2 = Unpooled.compositeBuffer();
    cb.addComponents(buf4, buf5, buf6);

    // cb.c
    // cb2.writerIndex(128 * 1024);

    cb.addComponent(cb2);

    Long number = cb2.readLong(); // causes IllegalBufferAccessException
                                  // here!

}

From source file:com.wotifgroup.zopfli.JopfliDeflater.java

License:Apache License

@Override
public void reset() {
    this.bp = new CharByReference((char) 0);
    this.out = new PointerByReference(new Pointer(0));
    this.outSize = new NativeSizeByReference();
    this.currentOutOffset = new NativeSize(0);
    this.buf = Unpooled.compositeBuffer();
    this.state = this.zlibWrapping ? State.HEADER : State.DEFLATING;
    this.eof = false;
    this.headerState = 1;
    this.trailerState = 1;
    this.adler32.reset();
}

From source file:divconq.http.multipart.InternalAttribute.java

License:Apache License

public ByteBuf toByteBuf() {
    return Unpooled.compositeBuffer().addComponents(value).writerIndex(size()).readerIndex(0);
}

From source file:io.advantageous.conekt.http.impl.HttpClientRequestImpl.java

License:Open Source License

private void write(ByteBuf buff, boolean end) {
    int readableBytes = buff.readableBytes();
    if (readableBytes == 0 && !end) {
        // nothing to write to the connection just return
        return;//from  w  w  w  .j ava  2s  .c o  m
    }

    if (end) {
        completed = true;
    }
    if (!end && !chunked && !contentLengthSet()) {
        throw new IllegalStateException(
                "You must set the Content-Length header to be the total size of the message "
                        + "body BEFORE sending any data if you are not using HTTP chunked encoding.");
    }

    written += buff.readableBytes();
    if (conn == null) {
        if (pendingChunks == null) {
            pendingChunks = buff;
        } else {
            CompositeByteBuf pending;
            if (pendingChunks instanceof CompositeByteBuf) {
                pending = (CompositeByteBuf) pendingChunks;
            } else {
                pending = Unpooled.compositeBuffer();
                pending.addComponent(pendingChunks).writerIndex(pendingChunks.writerIndex());
                pendingChunks = pending;
            }
            pending.addComponent(buff).writerIndex(pending.writerIndex() + buff.writerIndex());
        }
        connect();
    } else {
        if (!headWritten) {
            writeHeadWithContent(buff, end);
        } else {
            if (end) {
                if (buff.isReadable()) {
                    conn.writeToChannel(new DefaultLastHttpContent(buff, false));
                } else {
                    conn.writeToChannel(LastHttpContent.EMPTY_LAST_CONTENT);
                }
            } else {
                conn.writeToChannel(new DefaultHttpContent(buff));
            }
        }
        if (end) {
            conn.reportBytesWritten(written);

            if (respHandler != null) {
                conn.endRequest();
            }
        }
    }
}

From source file:io.grpc.netty.NettyHandlerTestBase.java

License:Apache License

protected final ByteBuf captureWrite(ChannelHandlerContext ctx) {
    ArgumentCaptor<ByteBuf> captor = ArgumentCaptor.forClass(ByteBuf.class);
    verify(ctx, atLeastOnce()).write(captor.capture(), any(ChannelPromise.class));
    CompositeByteBuf composite = Unpooled.compositeBuffer();
    for (ByteBuf buf : captor.getAllValues()) {
        composite.addComponent(buf);//from  w w  w  .  jav a2 s.  c  o  m
        composite.writerIndex(composite.writerIndex() + buf.readableBytes());
    }
    return composite;
}

From source file:io.jsync.http.impl.DefaultHttpClientRequest.java

License:Open Source License

private synchronized DefaultHttpClientRequest write(ByteBuf buff, boolean end) {
    int readableBytes = buff.readableBytes();
    if (readableBytes == 0 && !end) {
        // nothing to write to the connection just return
        return this;
    }// w  ww  .ja va 2 s . co m

    if (end) {
        completed = true;
    }

    written += buff.readableBytes();

    if (!end && !raw && !chunked && !contentLengthSet()) {
        throw new IllegalStateException(
                "You must set the Content-Length header to be the total size of the message "
                        + "body BEFORE sending any data if you are not using HTTP chunked encoding.");
    }

    if (conn == null) {
        if (pendingChunks == null) {
            pendingChunks = buff;
        } else {
            CompositeByteBuf pending;
            if (pendingChunks instanceof CompositeByteBuf) {
                pending = (CompositeByteBuf) pendingChunks;
            } else {
                pending = Unpooled.compositeBuffer();
                pending.addComponent(pendingChunks).writerIndex(pendingChunks.writerIndex());
                pendingChunks = pending;
            }
            pending.addComponent(buff).writerIndex(pending.writerIndex() + buff.writerIndex());
        }
        connect();
    } else {
        if (!headWritten) {
            writeHeadWithContent(buff, end);
        } else {
            if (end) {
                writeEndChunk(buff);
            } else {
                sendChunk(buff);
            }
        }
        if (end) {
            conn.endRequest();
        }
    }
    return this;
}