Search in sources :

Example 21 with CompositeByteBuf

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

the class AbstractIntegrationTest method testIdentity.

protected void testIdentity(final byte[] data) {
    final ByteBuf in = Unpooled.wrappedBuffer(data);
    assertTrue(encoder.writeOutbound(in.retain()));
    assertTrue(encoder.finish());
    final CompositeByteBuf compressed = Unpooled.compositeBuffer();
    ByteBuf msg;
    while ((msg = encoder.readOutbound()) != null) {
        compressed.addComponent(true, msg);
    }
    assertThat(compressed, is(notNullValue()));
    decoder.writeInbound(compressed.retain());
    assertFalse(compressed.isReadable());
    final CompositeByteBuf decompressed = Unpooled.compositeBuffer();
    while ((msg = decoder.readInbound()) != null) {
        decompressed.addComponent(true, msg);
    }
    assertEquals(in.resetReaderIndex(), decompressed);
    compressed.release();
    decompressed.release();
    in.release();
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 22 with CompositeByteBuf

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

the class FastLzIntegrationTest method testIdentity.

// test batched flow of data
@Override
protected void testIdentity(final byte[] data) {
    final ByteBuf original = Unpooled.wrappedBuffer(data);
    int written = 0, length = rand.nextInt(100);
    while (written + length < data.length) {
        ByteBuf in = Unpooled.wrappedBuffer(data, written, length);
        encoder.writeOutbound(in);
        written += length;
        length = rand.nextInt(100);
    }
    ByteBuf in = Unpooled.wrappedBuffer(data, written, data.length - written);
    encoder.writeOutbound(in);
    encoder.finish();
    ByteBuf msg;
    final CompositeByteBuf compressed = Unpooled.compositeBuffer();
    while ((msg = encoder.readOutbound()) != null) {
        compressed.addComponent(true, msg);
    }
    assertThat(compressed, is(notNullValue()));
    final byte[] compressedArray = new byte[compressed.readableBytes()];
    compressed.readBytes(compressedArray);
    written = 0;
    length = rand.nextInt(100);
    while (written + length < compressedArray.length) {
        in = Unpooled.wrappedBuffer(compressedArray, written, length);
        decoder.writeInbound(in);
        written += length;
        length = rand.nextInt(100);
    }
    in = Unpooled.wrappedBuffer(compressedArray, written, compressedArray.length - written);
    decoder.writeInbound(in);
    assertFalse(compressed.isReadable());
    final CompositeByteBuf decompressed = Unpooled.compositeBuffer();
    while ((msg = decoder.readInbound()) != null) {
        decompressed.addComponent(true, msg);
    }
    assertEquals(original, decompressed);
    compressed.release();
    decompressed.release();
    original.release();
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 23 with CompositeByteBuf

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

the class AbstractDecoderTest method readDecompressed.

protected 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 24 with CompositeByteBuf

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

the class SslHandler method wrap.

private SSLEngineResult wrap(ByteBufAllocator alloc, SSLEngine engine, ByteBuf in, ByteBuf out) throws SSLException {
    ByteBuf newDirectIn = null;
    try {
        int readerIndex = in.readerIndex();
        int readableBytes = in.readableBytes();
        // We will call SslEngine.wrap(ByteBuffer[], ByteBuffer) to allow efficient handling of
        // CompositeByteBuf without force an extra memory copy when CompositeByteBuffer.nioBuffer() is called.
        final ByteBuffer[] in0;
        if (in.isDirect() || !engineType.wantsDirectBuffer) {
            // which is better then walking the composed ByteBuf in most cases.
            if (!(in instanceof CompositeByteBuf) && in.nioBufferCount() == 1) {
                in0 = singleBuffer;
                // We know its only backed by 1 ByteBuffer so use internalNioBuffer to keep object allocation
                // to a minimum.
                in0[0] = in.internalNioBuffer(readerIndex, readableBytes);
            } else {
                in0 = in.nioBuffers();
            }
        } else {
            // We could even go further here and check if its a CompositeByteBuf and if so try to decompose it and
            // only replace the ByteBuffer that are not direct. At the moment we just will replace the whole
            // CompositeByteBuf to keep the complexity to a minimum
            newDirectIn = alloc.directBuffer(readableBytes);
            newDirectIn.writeBytes(in, readerIndex, readableBytes);
            in0 = singleBuffer;
            in0[0] = newDirectIn.internalNioBuffer(newDirectIn.readerIndex(), readableBytes);
        }
        for (; ; ) {
            ByteBuffer out0 = out.nioBuffer(out.writerIndex(), out.writableBytes());
            SSLEngineResult result = engine.wrap(in0, out0);
            in.skipBytes(result.bytesConsumed());
            out.writerIndex(out.writerIndex() + result.bytesProduced());
            switch(result.getStatus()) {
                case BUFFER_OVERFLOW:
                    out.ensureWritable(maxPacketBufferSize);
                    break;
                default:
                    return result;
            }
        }
    } finally {
        // Null out to allow GC of ByteBuffer
        singleBuffer[0] = null;
        if (newDirectIn != null) {
            newDirectIn.release();
        }
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) SSLEngineResult(javax.net.ssl.SSLEngineResult) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 25 with CompositeByteBuf

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

the class EpollDatagramChannel method doWriteMessage.

private boolean doWriteMessage(Object msg) throws Exception {
    final ByteBuf data;
    InetSocketAddress remoteAddress;
    if (msg instanceof AddressedEnvelope) {
        @SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, InetSocketAddress> envelope = (AddressedEnvelope<ByteBuf, InetSocketAddress>) msg;
        data = envelope.content();
        remoteAddress = envelope.recipient();
    } else {
        data = (ByteBuf) msg;
        remoteAddress = null;
    }
    final int dataLen = data.readableBytes();
    if (dataLen == 0) {
        return true;
    }
    if (remoteAddress == null) {
        remoteAddress = remote;
        if (remoteAddress == null) {
            throw new NotYetConnectedException();
        }
    }
    final int writtenBytes;
    if (data.hasMemoryAddress()) {
        long memoryAddress = data.memoryAddress();
        writtenBytes = fd().sendToAddress(memoryAddress, data.readerIndex(), data.writerIndex(), remoteAddress.getAddress(), remoteAddress.getPort());
    } else if (data instanceof CompositeByteBuf) {
        IovArray array = ((EpollEventLoop) eventLoop()).cleanArray();
        array.add(data);
        int cnt = array.count();
        assert cnt != 0;
        writtenBytes = fd().sendToAddresses(array.memoryAddress(0), cnt, remoteAddress.getAddress(), remoteAddress.getPort());
    } else {
        ByteBuffer nioData = data.internalNioBuffer(data.readerIndex(), data.readableBytes());
        writtenBytes = fd().sendTo(nioData, nioData.position(), nioData.limit(), remoteAddress.getAddress(), remoteAddress.getPort());
    }
    return writtenBytes > 0;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) InetSocketAddress(java.net.InetSocketAddress) NotYetConnectedException(java.nio.channels.NotYetConnectedException) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuffer(java.nio.ByteBuffer)

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