Search in sources :

Example 16 with CompositeByteBuf

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

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

use of io.netty.buffer.CompositeByteBuf in project grpc-java by grpc.

the class NettyHandlerTestBase method captureWrite.

protected final ByteBuf captureWrite(ChannelHandlerContext ctx) {
    ArgumentCaptor<ByteBuf> captor = ArgumentCaptor.forClass(ByteBuf.class);
    verify(ctx, atLeastOnce()).write(captor.capture(), any(ChannelPromise.class));
    CompositeByteBuf composite = Unpooled.compositeBuffer();
    for (ByteBuf buf : captor.getAllValues()) {
        composite.addComponent(buf);
        composite.writerIndex(composite.writerIndex() + buf.readableBytes());
    }
    return composite;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Example 19 with CompositeByteBuf

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

the class DeflateEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
    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.length;
        compressedContent = fullCompressedContent.slice(0, realLength);
    } else {
        compressedContent = fullCompressedContent;
    }
    WebSocketFrame outMsg;
    if (msg instanceof TextWebSocketFrame) {
        outMsg = new TextWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else if (msg instanceof BinaryWebSocketFrame) {
        outMsg = new BinaryWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else if (msg instanceof ContinuationWebSocketFrame) {
        outMsg = new ContinuationWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else {
        throw new CodecException("unexpected frame type: " + msg.getClass().getName());
    }
    out.add(outMsg);
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) CodecException(io.netty.handler.codec.CodecException) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 20 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 {
    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 && ((ByteBufHolder) m).content().isReadable()) {
                aggregated = beginAggregation(m, ((ByteBufHolder) m).content().retain());
            } else {
                aggregated = beginAggregation(m, EMPTY_BUFFER);
            }
            finishAggregation(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) {
            finishAggregation(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)

Aggregations

CompositeByteBuf (io.netty.buffer.CompositeByteBuf)37 ByteBuf (io.netty.buffer.ByteBuf)28 ChannelFuture (io.netty.channel.ChannelFuture)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 IOException (java.io.IOException)4 ChannelFutureListener (io.netty.channel.ChannelFutureListener)3 CodecException (io.netty.handler.codec.CodecException)3 InetSocketAddress (java.net.InetSocketAddress)3 ByteBuffer (java.nio.ByteBuffer)3 AddressedEnvelope (io.netty.channel.AddressedEnvelope)2 Channel (io.netty.channel.Channel)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelPromise (io.netty.channel.ChannelPromise)2 DefaultAddressedEnvelope (io.netty.channel.DefaultAddressedEnvelope)2 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)2 ContinuationWebSocketFrame (io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame)2 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)2 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)2 ArrayList (java.util.ArrayList)2 CodedOutputStream (com.google.protobuf.CodedOutputStream)1