Search in sources :

Example 26 with CompositeByteBuf

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

the class ChannelOutboundBufferTest method testNioBuffersExpand2.

@Test
public void testNioBuffersExpand2() {
    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());
    }
    buffer.addMessage(comp, comp.readableBytes(), channel.voidPromise());
    assertEquals(0, buffer.nioBufferCount(), "Should still be 0 as not flushed yet");
    buffer.addFlush();
    ByteBuffer[] buffers = buffer.nioBuffers();
    assertEquals(65, buffer.nioBufferCount());
    for (int i = 0; i < buffer.nioBufferCount(); i++) {
        if (i < 65) {
            assertEquals(buffers[i], buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()));
        } else {
            assertNull(buffers[i]);
        }
    }
    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 27 with CompositeByteBuf

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

the class DeflateEncoder method compressContent.

private ByteBuf compressContent(ChannelHandlerContext ctx, WebSocketFrame msg) {
    if (encoder == null) {
        encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, compressionLevel, windowSize, 8));
    }
    encoder.writeOutbound(msg.content().retain());
    CompositeByteBuf fullCompressedContent = ctx.alloc().compositeBuffer();
    for (; ; ) {
        ByteBuf partCompressedContent = encoder.readOutbound();
        if (partCompressedContent == null) {
            break;
        }
        if (!partCompressedContent.isReadable()) {
            partCompressedContent.release();
            continue;
        }
        fullCompressedContent.addComponent(true, partCompressedContent);
    }
    if (fullCompressedContent.numComponents() <= 0) {
        fullCompressedContent.release();
        throw new CodecException("cannot read compressed buffer");
    }
    if (msg.isFinalFragment() && noContext) {
        cleanup();
    }
    ByteBuf compressedContent;
    if (removeFrameTail(msg)) {
        int realLength = fullCompressedContent.readableBytes() - FRAME_TAIL.readableBytes();
        compressedContent = fullCompressedContent.slice(0, realLength);
    } else {
        compressedContent = fullCompressedContent;
    }
    return compressedContent;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) CodecException(io.netty.handler.codec.CodecException) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 28 with CompositeByteBuf

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

the class BufUnwrapperTest method readableNioBuffers_worksWithComposite.

@Test
public void readableNioBuffers_worksWithComposite() {
    CompositeByteBuf buf = alloc.compositeBuffer();
    buf.addComponent(true, alloc.buffer(1).writeByte('a'));
    try (BufUnwrapper unwrapper = new BufUnwrapper()) {
        ByteBuffer[] internalBufs = unwrapper.readableNioBuffers(buf);
        Truth.assertThat(internalBufs).hasLength(1);
        assertEquals('a', internalBufs[0].get(0));
    } finally {
        buf.release();
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 29 with CompositeByteBuf

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

the class BufUnwrapperTest method writableNioBuffers_worksWithComposite.

@Test
public void writableNioBuffers_worksWithComposite() {
    CompositeByteBuf buf = alloc.compositeBuffer();
    buf.addComponent(alloc.buffer(1));
    buf.capacity(1);
    try (BufUnwrapper unwrapper = new BufUnwrapper()) {
        ByteBuffer[] internalBufs = unwrapper.writableNioBuffers(buf);
        Truth.assertThat(internalBufs).hasLength(1);
        internalBufs[0].put((byte) 'a');
        buf.writerIndex(1);
        assertEquals('a', buf.readByte());
    } finally {
        buf.release();
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 30 with CompositeByteBuf

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

the class NettyReadableBufferTest method getByteBufferFromCompositeBufferReturnsOnlyFirstComponent.

@Test
public void getByteBufferFromCompositeBufferReturnsOnlyFirstComponent() {
    CompositeByteBuf composite = Unpooled.compositeBuffer(10);
    int chunks = 4;
    int chunkLen = msg.length() / chunks;
    for (String chunk : Splitter.fixedLength(chunkLen).split(msg)) {
        composite.addComponent(true, Unpooled.copiedBuffer(chunk.getBytes(UTF_8)));
    }
    buffer = new NettyReadableBuffer(composite);
    byte[] array = new byte[chunkLen];
    buffer.getByteBuffer().get(array);
    assertArrayEquals(msg.substring(0, chunkLen).getBytes(UTF_8), array);
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) Test(org.junit.Test)

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