Search in sources :

Example 36 with CompositeByteBuf

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

the class Netty4Utils method toByteBuf.

/**
     * 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.
     */
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);
        }
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) BytesRefIterator(org.apache.lucene.util.BytesRefIterator) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) BytesRef(org.apache.lucene.util.BytesRef)

Example 37 with CompositeByteBuf

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

the class VertxHandler method safeBuffer.

public static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) {
    if (buf == Unpooled.EMPTY_BUFFER) {
        return buf;
    }
    if (buf.isDirect() || buf instanceof CompositeByteBuf) {
        try {
            if (buf.isReadable()) {
                ByteBuf buffer = allocator.heapBuffer(buf.readableBytes());
                buffer.writeBytes(buf);
                return buffer;
            } else {
                return Unpooled.EMPTY_BUFFER;
            }
        } finally {
            buf.release();
        }
    }
    return buf;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 38 with CompositeByteBuf

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

the class HttpClientRequestImpl method write.

private void write(ByteBuf buff, boolean end) {
    if (buff == null && !end) {
        // nothing to write to the connection just return
        return;
    }
    if (end) {
        if (buff != null && !chunked && !contentLengthSet()) {
            headers().set(CONTENT_LENGTH, String.valueOf(buff.writerIndex()));
        }
    } else {
        if (!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 (buff != null) {
        written += buff.readableBytes();
        if (followRedirects > 0) {
            if (cachedChunks == null) {
                cachedChunks = Unpooled.compositeBuffer();
            }
            cachedChunks.addComponent(buff).writerIndex(cachedChunks.writerIndex() + buff.writerIndex());
        }
    }
    if (stream == null) {
        if (buff != 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(null);
    } else {
        if (!headWritten) {
            writeHeadWithContent(buff, end);
        } else {
            stream.writeBuffer(buff, end);
        }
        if (end) {
            stream.connection().reportBytesWritten(written);
            if (respHandler != null) {
                stream.endRequest();
            }
        }
    }
    if (end) {
        completed = true;
        if (completionHandler != null) {
            completionHandler.handle(null);
        }
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Example 39 with CompositeByteBuf

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

the class AbstractIntegrationTest method testIdentity.

protected void testIdentity(final byte[] data) {
    final ByteBuf in = Unpooled.wrappedBuffer(data);
    assertTrue(encoder.writeOutbound(in.retain()));
    assertTrue(encoder.finish());
    final CompositeByteBuf compressed = Unpooled.compositeBuffer();
    ByteBuf msg;
    while ((msg = encoder.readOutbound()) != null) {
        compressed.addComponent(true, msg);
    }
    assertThat(compressed, is(notNullValue()));
    decoder.writeInbound(compressed.retain());
    assertFalse(compressed.isReadable());
    final CompositeByteBuf decompressed = Unpooled.compositeBuffer();
    while ((msg = decoder.readInbound()) != null) {
        decompressed.addComponent(true, msg);
    }
    assertEquals(in.resetReaderIndex(), decompressed);
    compressed.release();
    decompressed.release();
    in.release();
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 40 with CompositeByteBuf

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

the class FastLzIntegrationTest method testIdentity.

// test batched flow of data
@Override
protected void testIdentity(final byte[] data) {
    final ByteBuf original = Unpooled.wrappedBuffer(data);
    int written = 0, length = rand.nextInt(100);
    while (written + length < data.length) {
        ByteBuf in = Unpooled.wrappedBuffer(data, written, length);
        encoder.writeOutbound(in);
        written += length;
        length = rand.nextInt(100);
    }
    ByteBuf in = Unpooled.wrappedBuffer(data, written, data.length - written);
    encoder.writeOutbound(in);
    encoder.finish();
    ByteBuf msg;
    final CompositeByteBuf compressed = Unpooled.compositeBuffer();
    while ((msg = encoder.readOutbound()) != null) {
        compressed.addComponent(true, msg);
    }
    assertThat(compressed, is(notNullValue()));
    final byte[] compressedArray = new byte[compressed.readableBytes()];
    compressed.readBytes(compressedArray);
    written = 0;
    length = rand.nextInt(100);
    while (written + length < compressedArray.length) {
        in = Unpooled.wrappedBuffer(compressedArray, written, length);
        decoder.writeInbound(in);
        written += length;
        length = rand.nextInt(100);
    }
    in = Unpooled.wrappedBuffer(compressedArray, written, compressedArray.length - written);
    decoder.writeInbound(in);
    assertFalse(compressed.isReadable());
    final CompositeByteBuf decompressed = Unpooled.compositeBuffer();
    while ((msg = decoder.readInbound()) != null) {
        decompressed.addComponent(true, msg);
    }
    assertEquals(original, decompressed);
    compressed.release();
    decompressed.release();
    original.release();
}
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