Search in sources :

Example 61 with CompositeByteBuf

use of io.netty.buffer.CompositeByteBuf in project netty by netty.

the class BrotliEncoderTest method readDecompressed.

@Override
protected ByteBuf readDecompressed(final int dataLength) throws Exception {
    CompositeByteBuf decompressed = Unpooled.compositeBuffer();
    ByteBuf msg;
    while ((msg = channel.readOutbound()) != null) {
        if (msg.isReadable()) {
            decompressed.addComponent(true, decompress(msg, -1));
        } else {
            msg.release();
        }
    }
    return decompressed;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 62 with CompositeByteBuf

use of io.netty.buffer.CompositeByteBuf in project netty by netty.

the class MessageAggregator method decode.

@Override
protected void decode(final ChannelHandlerContext ctx, I msg, List<Object> out) throws Exception {
    assert aggregating;
    if (isStartMessage(msg)) {
        handlingOversizedMessage = false;
        if (currentMessage != null) {
            currentMessage.release();
            currentMessage = null;
            throw new MessageAggregationException();
        }
        @SuppressWarnings("unchecked") S m = (S) msg;
        // Send the continue response if necessary (e.g. 'Expect: 100-continue' header)
        // Check before content length. Failing an expectation may result in a different response being sent.
        Object continueResponse = newContinueResponse(m, maxContentLength, ctx.pipeline());
        if (continueResponse != null) {
            // Cache the write listener for reuse.
            ChannelFutureListener listener = continueResponseWriteListener;
            if (listener == null) {
                continueResponseWriteListener = listener = new ChannelFutureListener() {

                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        if (!future.isSuccess()) {
                            ctx.fireExceptionCaught(future.cause());
                        }
                    }
                };
            }
            // Make sure to call this before writing, otherwise reference counts may be invalid.
            boolean closeAfterWrite = closeAfterContinueResponse(continueResponse);
            handlingOversizedMessage = ignoreContentAfterContinueResponse(continueResponse);
            final ChannelFuture future = ctx.writeAndFlush(continueResponse).addListener(listener);
            if (closeAfterWrite) {
                future.addListener(ChannelFutureListener.CLOSE);
                return;
            }
            if (handlingOversizedMessage) {
                return;
            }
        } else if (isContentLengthInvalid(m, maxContentLength)) {
            // if content length is set, preemptively close if it's too large
            invokeHandleOversizedMessage(ctx, m);
            return;
        }
        if (m instanceof DecoderResultProvider && !((DecoderResultProvider) m).decoderResult().isSuccess()) {
            O aggregated;
            if (m instanceof ByteBufHolder) {
                aggregated = beginAggregation(m, ((ByteBufHolder) m).content().retain());
            } else {
                aggregated = beginAggregation(m, EMPTY_BUFFER);
            }
            finishAggregation0(aggregated);
            out.add(aggregated);
            return;
        }
        // A streamed message - initialize the cumulative buffer, and wait for incoming chunks.
        CompositeByteBuf content = ctx.alloc().compositeBuffer(maxCumulationBufferComponents);
        if (m instanceof ByteBufHolder) {
            appendPartialContent(content, ((ByteBufHolder) m).content());
        }
        currentMessage = beginAggregation(m, content);
    } else if (isContentMessage(msg)) {
        if (currentMessage == null) {
            // until the begging of the next request/response.
            return;
        }
        // Merge the received chunk into the content of the current message.
        CompositeByteBuf content = (CompositeByteBuf) currentMessage.content();
        @SuppressWarnings("unchecked") final C m = (C) msg;
        // Handle oversized message.
        if (content.readableBytes() > maxContentLength - m.content().readableBytes()) {
            // By convention, full message type extends first message type.
            @SuppressWarnings("unchecked") S s = (S) currentMessage;
            invokeHandleOversizedMessage(ctx, s);
            return;
        }
        // Append the content of the chunk.
        appendPartialContent(content, m.content());
        // Give the subtypes a chance to merge additional information such as trailing headers.
        aggregate(currentMessage, m);
        final boolean last;
        if (m instanceof DecoderResultProvider) {
            DecoderResult decoderResult = ((DecoderResultProvider) m).decoderResult();
            if (!decoderResult.isSuccess()) {
                if (currentMessage instanceof DecoderResultProvider) {
                    ((DecoderResultProvider) currentMessage).setDecoderResult(DecoderResult.failure(decoderResult.cause()));
                }
                last = true;
            } else {
                last = isLastContentMessage(m);
            }
        } else {
            last = isLastContentMessage(m);
        }
        if (last) {
            finishAggregation0(currentMessage);
            // All done
            out.add(currentMessage);
            currentMessage = null;
        }
    } else {
        throw new MessageAggregationException();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBufHolder(io.netty.buffer.ByteBufHolder)

Example 63 with CompositeByteBuf

use of 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 64 with CompositeByteBuf

use of 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 65 with CompositeByteBuf

use of 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)

Aggregations

CompositeByteBuf (io.netty.buffer.CompositeByteBuf)85 ByteBuf (io.netty.buffer.ByteBuf)65 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)9 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 InetSocketAddress (java.net.InetSocketAddress)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 ExecutionException (java.util.concurrent.ExecutionException)3 CodedOutputStream (com.google.protobuf.CodedOutputStream)2 Bootstrap (io.netty.bootstrap.Bootstrap)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2