Search in sources :

Example 6 with CompositeByteBuf

use of 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("Should still be 0 as not flushed yet", 0, buffer.nioBufferCount());
    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.Test)

Example 7 with CompositeByteBuf

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

the class CoalescingBufferQueue method compose.

/**
     * Compose the current buffer with another.
     */
private ByteBuf compose(ByteBuf current, ByteBuf next) {
    if (current == null) {
        return next;
    }
    if (current instanceof CompositeByteBuf) {
        CompositeByteBuf composite = (CompositeByteBuf) current;
        composite.addComponent(true, next);
        return composite;
    }
    // Create a composite buffer to accumulate this pair and potentially all the buffers
    // in the queue. Using +2 as we have already dequeued current and next.
    CompositeByteBuf composite = channel.alloc().compositeBuffer(bufAndListenerPairs.size() + 2);
    composite.addComponent(true, current);
    composite.addComponent(true, next);
    return composite;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Example 8 with CompositeByteBuf

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

the class DeflateDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
    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();
    decoder.writeInbound(msg.content().retain());
    if (appendFrameTail(msg)) {
        decoder.writeInbound(Unpooled.wrappedBuffer(FRAME_TAIL));
    }
    CompositeByteBuf compositeUncompressedContent = ctx.alloc().compositeBuffer();
    for (; ; ) {
        ByteBuf partUncompressedContent = decoder.readInbound();
        if (partUncompressedContent == null) {
            break;
        }
        if (!partUncompressedContent.isReadable()) {
            partUncompressedContent.release();
            continue;
        }
        compositeUncompressedContent.addComponent(true, partUncompressedContent);
    }
    // See https://github.com/netty/netty/issues/4348
    if (readable && compositeUncompressedContent.numComponents() <= 0) {
        compositeUncompressedContent.release();
        throw new CodecException("cannot read uncompressed buffer");
    }
    if (msg.isFinalFragment() && noContext) {
        cleanup();
    }
    WebSocketFrame outMsg;
    if (msg instanceof TextWebSocketFrame) {
        outMsg = new TextWebSocketFrame(msg.isFinalFragment(), newRsv(msg), compositeUncompressedContent);
    } else if (msg instanceof BinaryWebSocketFrame) {
        outMsg = new BinaryWebSocketFrame(msg.isFinalFragment(), newRsv(msg), compositeUncompressedContent);
    } else if (msg instanceof ContinuationWebSocketFrame) {
        outMsg = new ContinuationWebSocketFrame(msg.isFinalFragment(), newRsv(msg), compositeUncompressedContent);
    } 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) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) 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 9 with CompositeByteBuf

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

the class AbstractEncoderTest method readDecompressed.

protected ByteBuf readDecompressed(final int dataLength) throws Exception {
    CompositeByteBuf compressed = Unpooled.compositeBuffer();
    ByteBuf msg;
    while ((msg = channel.readOutbound()) != null) {
        compressed.addComponent(true, msg);
    }
    return decompress(compressed, dataLength);
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 10 with CompositeByteBuf

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

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