Search in sources :

Example 1 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project elasticsearch by elastic.

the class Netty4UtilsTests method testToChannelBuffer.

public void testToChannelBuffer() throws IOException {
    BytesReference ref = getRandomizedBytesReference(randomIntBetween(1, 3 * PAGE_SIZE));
    ByteBuf buffer = Netty4Utils.toByteBuf(ref);
    BytesReference bytesReference = Netty4Utils.toBytesReference(buffer);
    if (ref instanceof ByteBufBytesReference) {
        assertEquals(buffer, ((ByteBufBytesReference) ref).toByteBuf());
    } else if (AbstractBytesReferenceTestCase.getNumPages(ref) > 1) {
        // we gather the buffers into a channel buffer
        assertTrue(buffer instanceof CompositeByteBuf);
    }
    assertArrayEquals(BytesReference.toBytes(ref), BytesReference.toBytes(bytesReference));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project vert.x by eclipse.

the class Http2ConnectionBase method safeBuffer.

/**
   * Return a buffer from HTTP/2 codec that Vert.x can use:
   *
   * - if it's a direct buffer (coming likely from OpenSSL) : we get a heap buffer version
   * - if it's a composite buffer we do the same
   * - otherwise we increase the ref count
   */
static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) {
    if (buf == Unpooled.EMPTY_BUFFER) {
        return buf;
    }
    if (buf.isDirect() || buf instanceof CompositeByteBuf) {
        if (buf.isReadable()) {
            ByteBuf buffer = allocator.heapBuffer(buf.readableBytes());
            buffer.writeBytes(buf);
            return buffer;
        } else {
            return Unpooled.EMPTY_BUFFER;
        }
    }
    return buf.retain();
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Example 3 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project netty by netty.

the class CoalescingBufferQueue method compose.

/**
     * Compose the current buffer with another.
     */
private ByteBuf compose(ByteBuf current, ByteBuf next) {
    if (current == null) {
        return next;
    }
    if (current instanceof CompositeByteBuf) {
        CompositeByteBuf composite = (CompositeByteBuf) current;
        composite.addComponent(true, next);
        return composite;
    }
    // Create a composite buffer to accumulate this pair and potentially all the buffers
    // in the queue. Using +2 as we have already dequeued current and next.
    CompositeByteBuf composite = channel.alloc().compositeBuffer(bufAndListenerPairs.size() + 2);
    composite.addComponent(true, current);
    composite.addComponent(true, next);
    return composite;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Example 4 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project java-in-action by xinghalo.

the class CompositeTest method main.

public static void main(String[] args) {
    // 组合缓冲区
    CompositeByteBuf compBuf = Unpooled.compositeBuffer();
    ByteBuf header = Unpooled.buffer(8);
    header.writeBytes("aaaabbbb".getBytes());
    ByteBuf body = Unpooled.buffer(8);
    body.writeBytes("11112222".getBytes());
    // 添加ByteBuf到CompositeByteBuf
    compBuf.addComponents(header, body);
    System.out.println(compBuf.toString());
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 5 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project x-pipe by ctripcorp.

the class ArrayParser method getWriteByteBuf.

@Override
protected ByteBuf getWriteByteBuf() {
    int length = payload.length;
    CompositeByteBuf result = new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, false, payload.length + 1);
    String prefix = String.format("%c%d\r\n", ASTERISK_BYTE, length);
    result.addComponent(Unpooled.wrappedBuffer(prefix.getBytes()));
    for (Object o : payload) {
        ByteBuf buff = ParserManager.parse(o);
        result.addComponent(buff);
    }
    result.setIndex(0, result.capacity());
    return result;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

CompositeByteBuf (io.netty.buffer.CompositeByteBuf)86 ByteBuf (io.netty.buffer.ByteBuf)65 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)8 ByteBuffer (java.nio.ByteBuffer)7 ChannelFuture (io.netty.channel.ChannelFuture)6 Channel (io.netty.channel.Channel)5 ChannelFutureListener (io.netty.channel.ChannelFutureListener)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 Test (org.junit.jupiter.api.Test)4 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)3 CodecException (io.netty.handler.codec.CodecException)3 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)3 InetSocketAddress (java.net.InetSocketAddress)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 List (java.util.List)3 ExecutionException (java.util.concurrent.ExecutionException)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3