Search in sources :

Example 56 with CompositeByteBuf

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

the class DefaultRockerRenderer method render.

@Override
public void render(Context context, RockerModel rockerModel) throws Exception {
    try {
        ArrayOfByteArraysOutput output = rockerModel.render(ArrayOfByteArraysOutput.FACTORY);
        List<byte[]> arrays = output.getArrays();
        ByteBuf byteBuf;
        int size = arrays.size();
        if (size == 0) {
            byteBuf = Unpooled.EMPTY_BUFFER;
        } else if (size == 1) {
            byteBuf = Unpooled.wrappedBuffer(arrays.get(0));
        } else {
            byteBuf = new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, false, size, Iterables.transform(arrays, Unpooled::wrappedBuffer));
        }
        AsciiString contentType = output.getContentType() == ContentType.HTML ? HTML : TEXT;
        context.getResponse().contentTypeIfNotSet(contentType).send(byteBuf);
    } catch (Exception e) {
        // This can be removed when the above issue is rectified.
        throw new RendererException("Error rendering template " + rockerModel.getClass().getName(), e);
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) RendererException(ratpack.render.RendererException) ArrayOfByteArraysOutput(com.fizzed.rocker.runtime.ArrayOfByteArraysOutput) AsciiString(io.netty.util.AsciiString) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) RendererException(ratpack.render.RendererException)

Example 57 with CompositeByteBuf

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

the class DirectMemoryCache method get.

@Override
public BufferView get(int address) {
    Exceptions.checkNotClosed(this.closed.get(), this);
    List<ByteBuf> readBuffers = new ArrayList<>();
    while (address != CacheLayout.NO_ADDRESS) {
        // Locate the Buffer-Block for the current address.
        int bufferId = this.layout.getBufferId(address);
        int blockId = this.layout.getBlockId(address);
        DirectMemoryBuffer b = this.buffers[bufferId];
        // Fetch the read data into our buffer collection and then set the address to the next in the chain.
        address = b.read(blockId, readBuffers);
    }
    if (readBuffers.isEmpty()) {
        // Couldn't read anything, so this address must not point to anything.
        return null;
    } else {
        // Compose the result and return it.
        ByteBuf first = readBuffers.get(0);
        ByteBuf result = readBuffers.size() == 1 ? first : new CompositeByteBuf(first.alloc(), false, readBuffers.size(), Lists.reverse(readBuffers));
        this.metrics.get(result.readableBytes());
        return new NonReleaseableByteBufWrapper(result);
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ArrayList(java.util.ArrayList) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 58 with CompositeByteBuf

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

the class GrpcSerializationUtils method getByteBufFromReadableBuffer.

/**
 * Gets a Netty buffer directly from a gRPC ReadableBuffer.
 *
 * @param buffer the input buffer
 * @return the raw ByteBuf, or null if the ByteBuf cannot be extracted
 */
public static ByteBuf getByteBufFromReadableBuffer(ReadableBuffer buffer) {
    if (!sZeroCopyReceiveSupported) {
        return null;
    }
    try {
        if (buffer instanceof CompositeReadableBuffer) {
            Queue<ReadableBuffer> buffers = (Queue<ReadableBuffer>) sCompositeBuffers.get(buffer);
            if (buffers.size() == 1) {
                return getByteBufFromReadableBuffer(buffers.peek());
            } else {
                CompositeByteBuf buf = PooledByteBufAllocator.DEFAULT.compositeBuffer();
                for (ReadableBuffer readableBuffer : buffers) {
                    ByteBuf subBuffer = getByteBufFromReadableBuffer(readableBuffer);
                    if (subBuffer == null) {
                        return null;
                    }
                    buf.addComponent(true, subBuffer);
                }
                return buf;
            }
        } else if (buffer.getClass().equals(sReadableByteBuf.getDeclaringClass())) {
            return (ByteBuf) sReadableByteBuf.get(buffer);
        }
    } catch (Exception e) {
        LOG.warn("Failed to get data buffer from stream: {}.", e.toString());
        return null;
    }
    return null;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeReadableBuffer(io.grpc.internal.CompositeReadableBuffer) CompositeReadableBuffer(io.grpc.internal.CompositeReadableBuffer) ReadableBuffer(io.grpc.internal.ReadableBuffer) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) Queue(java.util.Queue)

Example 59 with CompositeByteBuf

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

the class HttpObjectAggregatorTest method checkContentBuffer.

private static void checkContentBuffer(FullHttpRequest aggregatedMessage) {
    CompositeByteBuf buffer = (CompositeByteBuf) aggregatedMessage.content();
    assertEquals(2, buffer.numComponents());
    List<ByteBuf> buffers = buffer.decompose(0, buffer.capacity());
    assertEquals(2, buffers.size());
    for (ByteBuf buf : buffers) {
        // This should be false as we decompose the buffer before to not have deep hierarchy
        assertFalse(buf instanceof CompositeByteBuf);
    }
    aggregatedMessage.release();
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Example 60 with CompositeByteBuf

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

the class SnappyFrameEncoderTest method testStreamStartIsOnlyWrittenOnce.

@Test
public void testStreamStartIsOnlyWrittenOnce() throws Exception {
    ByteBuf in = Unpooled.wrappedBuffer(new byte[] { 'n', 'e', 't', 't', 'y' });
    channel.writeOutbound(in.retain());
    // rewind the buffer to write the same data
    in.resetReaderIndex();
    channel.writeOutbound(in);
    assertTrue(channel.finish());
    ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { (byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, 0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, 0x2e, -0x47, 'n', 'e', 't', 't', 'y', 0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, 0x2e, -0x47, 'n', 'e', 't', 't', 'y' });
    CompositeByteBuf actual = Unpooled.compositeBuffer();
    for (; ; ) {
        ByteBuf m = channel.readOutbound();
        if (m == null) {
            break;
        }
        actual.addComponent(true, m);
    }
    assertEquals(expected, actual);
    expected.release();
    actual.release();
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) 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