Search in sources :

Example 16 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, boolean heapBuffer) {
    initChannels();
    final ByteBuf in = heapBuffer ? Unpooled.wrappedBuffer(data) : Unpooled.directBuffer(data.length).setBytes(0, data);
    final CompositeByteBuf compressed = Unpooled.compositeBuffer();
    final CompositeByteBuf decompressed = Unpooled.compositeBuffer();
    try {
        assertTrue(encoder.writeOutbound(in.retain()));
        assertTrue(encoder.finish());
        ByteBuf msg;
        while ((msg = encoder.readOutbound()) != null) {
            compressed.addComponent(true, msg);
        }
        assertThat(compressed, is(notNullValue()));
        decoder.writeInbound(compressed.retain());
        assertFalse(compressed.isReadable());
        while ((msg = decoder.readInbound()) != null) {
            decompressed.addComponent(true, msg);
        }
        in.readerIndex(0);
        assertEquals(in, decompressed);
    } finally {
        compressed.release();
        decompressed.release();
        in.release();
        closeChannels();
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 17 with CompositeByteBuf

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

the class LzmaFrameEncoderTest method testCompressionOfBatchedFlow.

@Override
protected void testCompressionOfBatchedFlow(final ByteBuf data) throws Exception {
    List<Integer> originalLengths = new ArrayList<Integer>();
    final int dataLength = data.readableBytes();
    int written = 0, length = rand.nextInt(50);
    while (written + length < dataLength) {
        ByteBuf in = data.retainedSlice(written, length);
        assertTrue(channel.writeOutbound(in));
        written += length;
        originalLengths.add(length);
        length = rand.nextInt(50);
    }
    length = dataLength - written;
    ByteBuf in = data.retainedSlice(written, dataLength - written);
    originalLengths.add(length);
    assertTrue(channel.writeOutbound(in));
    assertTrue(channel.finish());
    CompositeByteBuf decompressed = Unpooled.compositeBuffer();
    ByteBuf msg;
    int i = 0;
    while ((msg = channel.readOutbound()) != null) {
        ByteBuf decompressedMsg = decompress(msg, originalLengths.get(i++));
        decompressed.addComponent(true, decompressedMsg);
    }
    assertEquals(originalLengths.size(), i);
    assertEquals(data, decompressed);
    decompressed.release();
    data.release();
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ArrayList(java.util.ArrayList) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 18 with CompositeByteBuf

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

the class BrotliDecoderTest method readDecompressed.

private static ByteBuf readDecompressed(final EmbeddedChannel channel) {
    CompositeByteBuf decompressed = Unpooled.compositeBuffer();
    ByteBuf msg;
    while ((msg = channel.readInbound()) != null) {
        decompressed.addComponent(true, msg);
    }
    return decompressed;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 19 with CompositeByteBuf

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

the class ByteToMessageDecoderTest method releaseWhenCompositeCumulateThrows.

@Test
public void releaseWhenCompositeCumulateThrows() {
    final Error error = new Error();
    ByteBuf cumulation = new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, false, 64) {

        @Override
        public CompositeByteBuf addComponent(boolean increaseWriterIndex, ByteBuf buffer) {
            throw error;
        }

        @Override
        public CompositeByteBuf addFlattenedComponents(boolean increaseWriterIndex, ByteBuf buffer) {
            throw error;
        }
    }.writeZero(1);
    ByteBuf in = Unpooled.buffer().writeZero(12);
    try {
        ByteToMessageDecoder.COMPOSITE_CUMULATOR.cumulate(UnpooledByteBufAllocator.DEFAULT, cumulation, in);
        fail();
    } catch (Error expected) {
        assertSame(error, expected);
        assertEquals(0, in.refCnt());
        cumulation.release();
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) UnpooledHeapByteBuf(io.netty.buffer.UnpooledHeapByteBuf) Test(org.junit.jupiter.api.Test)

Example 20 with CompositeByteBuf

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

the class UnixChannelUtilTest method assertCompositeByteBufIsBufferCopyNeededForWrite.

private static void assertCompositeByteBufIsBufferCopyNeededForWrite(ByteBufAllocator alloc, int numDirect, int numHeap, boolean expected) {
    CompositeByteBuf comp = alloc.compositeBuffer(numDirect + numHeap);
    List<ByteBuf> byteBufs = new LinkedList<ByteBuf>();
    while (numDirect > 0) {
        byteBufs.add(alloc.directBuffer(1));
        numDirect--;
    }
    while (numHeap > 0) {
        byteBufs.add(alloc.heapBuffer(1));
        numHeap--;
    }
    Collections.shuffle(byteBufs);
    for (ByteBuf byteBuf : byteBufs) {
        comp.addComponent(byteBuf);
    }
    assertEquals(expected, isBufferCopyNeededForWrite(comp, IOV_MAX), byteBufs.toString());
    assertTrue(comp.release());
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) LinkedList(java.util.LinkedList)

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