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