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(int cIndex, Iterable<ByteBuf> buffers) 

Source Link

Document

Add the given ByteBuf s on the specific index Be aware that this method does not increase the writerIndex of the CompositeByteBuf .

Usage

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

License:Apache License

@Override
public void addContent(ByteBuf buffer, boolean last) throws IOException {
    if (buffer != null) {
        long localsize = buffer.readableBytes();
        checkSize(size + localsize);//from w ww  .  j  a v a 2 s.  c o m
        if (definedSize > 0 && definedSize < size + localsize) {
            throw new IOException("Out of size: " + (size + localsize) + " > " + definedSize);
        }
        size += localsize;
        if (byteBuf == null) {
            byteBuf = buffer;
        } else if (byteBuf instanceof CompositeByteBuf) {
            CompositeByteBuf cbb = (CompositeByteBuf) byteBuf;
            cbb.addComponent(buffer);
            cbb.writerIndex(cbb.writerIndex() + buffer.readableBytes());
        } else {
            CompositeByteBuf cbb = compositeBuffer(Integer.MAX_VALUE);
            cbb.addComponents(byteBuf, buffer);
            cbb.writerIndex(byteBuf.readableBytes() + buffer.readableBytes());
            byteBuf = cbb;
        }
    }
    if (last) {
        setCompleted();
    } else {
        if (buffer == null) {
            throw new NullPointerException("buffer");
        }
    }
}

From source file:org.elasticsearch.hadoop.transport.netty4.Netty4Utils.java

License:Apache License

/**
 * Turns the given BytesReference into a ByteBuf. Note: the returned ByteBuf will reference the internal
 * pages of the BytesReference. Don't free the bytes of reference before the ByteBuf goes out of scope.
 */// w w w  . j a va2 s  . c om
public static ByteBuf toByteBuf(final BytesReference reference) {
    if (reference.length() == 0) {
        return Unpooled.EMPTY_BUFFER;
    }
    if (reference instanceof ByteBufBytesReference) {
        return ((ByteBufBytesReference) reference).toByteBuf();
    } else {
        final BytesRefIterator iterator = reference.iterator();
        // usually we have one, two, or three components
        // from the header, the message, and a buffer
        final List<ByteBuf> buffers = new ArrayList<>(3);
        try {
            BytesRef slice;
            while ((slice = iterator.next()) != null) {
                buffers.add(Unpooled.wrappedBuffer(slice.bytes, slice.offset, slice.length));
            }
            final CompositeByteBuf composite = Unpooled.compositeBuffer(buffers.size());
            composite.addComponents(true, buffers);
            return composite;
        } catch (IOException ex) {
            throw new AssertionError("no IO happens here", ex);
        }
    }
}

From source file:org.elasticsearch.http.nio.ByteBufUtils.java

License:Apache License

/**
 * Turns the given BytesReference into a ByteBuf. Note: the returned ByteBuf will reference the internal
 * pages of the BytesReference. Don't free the bytes of reference before the ByteBuf goes out of scope.
 *//*from  w w  w.j a va2s  .com*/
static ByteBuf toByteBuf(final BytesReference reference) {
    if (reference.length() == 0) {
        return Unpooled.EMPTY_BUFFER;
    }
    if (reference instanceof ByteBufBytesReference) {
        return ((ByteBufBytesReference) reference).toByteBuf();
    } else {
        final BytesRefIterator iterator = reference.iterator();
        // usually we have one, two, or three components from the header, the message, and a buffer
        final List<ByteBuf> buffers = new ArrayList<>(3);
        try {
            BytesRef slice;
            while ((slice = iterator.next()) != null) {
                buffers.add(Unpooled.wrappedBuffer(slice.bytes, slice.offset, slice.length));
            }
            final CompositeByteBuf composite = Unpooled.compositeBuffer(buffers.size());
            composite.addComponents(true, buffers);
            return composite;
        } catch (IOException ex) {
            throw new AssertionError("no IO happens here", ex);
        }
    }
}