use of org.apache.flink.shaded.netty4.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(0, buffer.nioBufferCount(), "Should still be 0 as not flushed yet");
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 org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project netty by netty.
the class DeflateEncoder method compressContent.
private ByteBuf compressContent(ChannelHandlerContext ctx, WebSocketFrame msg) {
if (encoder == null) {
encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, compressionLevel, windowSize, 8));
}
encoder.writeOutbound(msg.content().retain());
CompositeByteBuf fullCompressedContent = ctx.alloc().compositeBuffer();
for (; ; ) {
ByteBuf partCompressedContent = encoder.readOutbound();
if (partCompressedContent == null) {
break;
}
if (!partCompressedContent.isReadable()) {
partCompressedContent.release();
continue;
}
fullCompressedContent.addComponent(true, partCompressedContent);
}
if (fullCompressedContent.numComponents() <= 0) {
fullCompressedContent.release();
throw new CodecException("cannot read compressed buffer");
}
if (msg.isFinalFragment() && noContext) {
cleanup();
}
ByteBuf compressedContent;
if (removeFrameTail(msg)) {
int realLength = fullCompressedContent.readableBytes() - FRAME_TAIL.readableBytes();
compressedContent = fullCompressedContent.slice(0, realLength);
} else {
compressedContent = fullCompressedContent;
}
return compressedContent;
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project grpc-java by grpc.
the class BufUnwrapperTest method readableNioBuffers_worksWithComposite.
@Test
public void readableNioBuffers_worksWithComposite() {
CompositeByteBuf buf = alloc.compositeBuffer();
buf.addComponent(true, alloc.buffer(1).writeByte('a'));
try (BufUnwrapper unwrapper = new BufUnwrapper()) {
ByteBuffer[] internalBufs = unwrapper.readableNioBuffers(buf);
Truth.assertThat(internalBufs).hasLength(1);
assertEquals('a', internalBufs[0].get(0));
} finally {
buf.release();
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project grpc-java by grpc.
the class BufUnwrapperTest method writableNioBuffers_worksWithComposite.
@Test
public void writableNioBuffers_worksWithComposite() {
CompositeByteBuf buf = alloc.compositeBuffer();
buf.addComponent(alloc.buffer(1));
buf.capacity(1);
try (BufUnwrapper unwrapper = new BufUnwrapper()) {
ByteBuffer[] internalBufs = unwrapper.writableNioBuffers(buf);
Truth.assertThat(internalBufs).hasLength(1);
internalBufs[0].put((byte) 'a');
buf.writerIndex(1);
assertEquals('a', buf.readByte());
} finally {
buf.release();
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project grpc-java by grpc.
the class NettyReadableBufferTest method getByteBufferFromCompositeBufferReturnsOnlyFirstComponent.
@Test
public void getByteBufferFromCompositeBufferReturnsOnlyFirstComponent() {
CompositeByteBuf composite = Unpooled.compositeBuffer(10);
int chunks = 4;
int chunkLen = msg.length() / chunks;
for (String chunk : Splitter.fixedLength(chunkLen).split(msg)) {
composite.addComponent(true, Unpooled.copiedBuffer(chunk.getBytes(UTF_8)));
}
buffer = new NettyReadableBuffer(composite);
byte[] array = new byte[chunkLen];
buffer.getByteBuffer().get(array);
assertArrayEquals(msg.substring(0, chunkLen).getBytes(UTF_8), array);
}
Aggregations