Search in sources :

Example 71 with CompositeByteBuf

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

the class DatagramUnicastTest method testSimpleSendCompositeDirectByteBuf.

public void testSimpleSendCompositeDirectByteBuf(Bootstrap sb, Bootstrap cb) throws Throwable {
    CompositeByteBuf buf = Unpooled.compositeBuffer();
    buf.addComponent(true, Unpooled.directBuffer().writeBytes(BYTES, 0, 2));
    buf.addComponent(true, Unpooled.directBuffer().writeBytes(BYTES, 2, 2));
    testSimpleSend(sb, cb, buf, true, BYTES, 1);
    CompositeByteBuf buf2 = Unpooled.compositeBuffer();
    buf2.addComponent(true, Unpooled.directBuffer().writeBytes(BYTES, 0, 2));
    buf2.addComponent(true, Unpooled.directBuffer().writeBytes(BYTES, 2, 2));
    testSimpleSend(sb, cb, buf2, true, BYTES, 4);
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Example 72 with CompositeByteBuf

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

the class CompositeBufferGatheringWriteTest method newCompositeBuffer.

private static ByteBuf newCompositeBuffer(ByteBufAllocator alloc) {
    CompositeByteBuf compositeByteBuf = alloc.compositeBuffer();
    compositeByteBuf.addComponent(true, alloc.directBuffer(4).writeInt(100));
    compositeByteBuf.addComponent(true, alloc.directBuffer(8).writeLong(123));
    compositeByteBuf.addComponent(true, alloc.directBuffer(8).writeLong(456));
    assertEquals(EXPECTED_BYTES, compositeByteBuf.readableBytes());
    return compositeByteBuf;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Example 73 with CompositeByteBuf

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

the class ChannelOutboundBufferTest method testNioBuffersMaxCount.

@Test
public void testNioBuffersMaxCount() {
    TestChannel channel = new TestChannel();
    ChannelOutboundBuffer buffer = new ChannelOutboundBuffer(channel);
    CompositeByteBuf comp = compositeBuffer(256);
    ByteBuf buf = directBuffer().writeBytes("buf1".getBytes(CharsetUtil.US_ASCII));
    for (int i = 0; i < 65; i++) {
        comp.addComponent(true, buf.copy());
    }
    assertEquals(65, comp.nioBufferCount());
    buffer.addMessage(comp, comp.readableBytes(), channel.voidPromise());
    assertEquals(0, buffer.nioBufferCount(), "Should still be 0 as not flushed yet");
    buffer.addFlush();
    // less than comp.nioBufferCount()
    final int maxCount = 10;
    ByteBuffer[] buffers = buffer.nioBuffers(maxCount, Integer.MAX_VALUE);
    assertTrue(buffer.nioBufferCount() <= maxCount, "Should not be greater than maxCount");
    for (int i = 0; i < buffer.nioBufferCount(); i++) {
        assertEquals(buffers[i], buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()));
    }
    release(buffer);
    buf.release();
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 74 with CompositeByteBuf

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

the class DeflateDecoder method decompressContent.

private ByteBuf decompressContent(ChannelHandlerContext ctx, WebSocketFrame msg) {
    if (decoder == null) {
        if (!(msg instanceof TextWebSocketFrame) && !(msg instanceof BinaryWebSocketFrame)) {
            throw new CodecException("unexpected initial frame type: " + msg.getClass().getName());
        }
        decoder = new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));
    }
    boolean readable = msg.content().isReadable();
    boolean emptyDeflateBlock = EMPTY_DEFLATE_BLOCK.equals(msg.content());
    decoder.writeInbound(msg.content().retain());
    if (appendFrameTail(msg)) {
        decoder.writeInbound(FRAME_TAIL.duplicate());
    }
    CompositeByteBuf compositeDecompressedContent = ctx.alloc().compositeBuffer();
    for (; ; ) {
        ByteBuf partUncompressedContent = decoder.readInbound();
        if (partUncompressedContent == null) {
            break;
        }
        if (!partUncompressedContent.isReadable()) {
            partUncompressedContent.release();
            continue;
        }
        compositeDecompressedContent.addComponent(true, partUncompressedContent);
    }
    // See https://github.com/netty/netty/issues/4348
    if (!emptyDeflateBlock && readable && compositeDecompressedContent.numComponents() <= 0) {
        // May contain left-over data that doesn't affect decompression
        if (!(msg instanceof ContinuationWebSocketFrame)) {
            compositeDecompressedContent.release();
            throw new CodecException("cannot read uncompressed buffer");
        }
    }
    if (msg.isFinalFragment() && noContext) {
        cleanup();
    }
    return compositeDecompressedContent;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) CodecException(io.netty.handler.codec.CodecException) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 75 with CompositeByteBuf

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

the class SslHandler method wrap.

private SSLEngineResult wrap(ByteBufAllocator alloc, SSLEngine engine, ByteBuf in, ByteBuf out) throws SSLException {
    ByteBuf newDirectIn = null;
    try {
        int readerIndex = in.readerIndex();
        int readableBytes = in.readableBytes();
        // We will call SslEngine.wrap(ByteBuffer[], ByteBuffer) to allow efficient handling of
        // CompositeByteBuf without force an extra memory copy when CompositeByteBuffer.nioBuffer() is called.
        final ByteBuffer[] in0;
        if (in.isDirect() || !engineType.wantsDirectBuffer) {
            // which is better then walking the composed ByteBuf in most cases.
            if (!(in instanceof CompositeByteBuf) && in.nioBufferCount() == 1) {
                in0 = singleBuffer;
                // We know its only backed by 1 ByteBuffer so use internalNioBuffer to keep object allocation
                // to a minimum.
                in0[0] = in.internalNioBuffer(readerIndex, readableBytes);
            } else {
                in0 = in.nioBuffers();
            }
        } else {
            // We could even go further here and check if its a CompositeByteBuf and if so try to decompose it and
            // only replace the ByteBuffer that are not direct. At the moment we just will replace the whole
            // CompositeByteBuf to keep the complexity to a minimum
            newDirectIn = alloc.directBuffer(readableBytes);
            newDirectIn.writeBytes(in, readerIndex, readableBytes);
            in0 = singleBuffer;
            in0[0] = newDirectIn.internalNioBuffer(newDirectIn.readerIndex(), readableBytes);
        }
        for (; ; ) {
            ByteBuffer out0 = out.nioBuffer(out.writerIndex(), out.writableBytes());
            SSLEngineResult result = engine.wrap(in0, out0);
            in.skipBytes(result.bytesConsumed());
            out.writerIndex(out.writerIndex() + result.bytesProduced());
            if (result.getStatus() == Status.BUFFER_OVERFLOW) {
                out.ensureWritable(engine.getSession().getPacketBufferSize());
            } else {
                return result;
            }
        }
    } finally {
        // Null out to allow GC of ByteBuffer
        singleBuffer[0] = null;
        if (newDirectIn != null) {
            newDirectIn.release();
        }
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) SSLEngineResult(javax.net.ssl.SSLEngineResult) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

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