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);
}
}
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);
}
}
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;
}
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();
}
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();
}
Aggregations