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, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y', 0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, '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();
}
use of io.netty.buffer.CompositeByteBuf in project netty by netty.
the class SslHandlerTest method compositeBufSizeEstimationGuaranteesSynchronousWrite.
private void compositeBufSizeEstimationGuaranteesSynchronousWrite(SslProvider serverProvider, SslProvider clientProvider) throws CertificateException, SSLException, ExecutionException, InterruptedException {
SelfSignedCertificate ssc = new SelfSignedCertificate();
final SslContext sslServerCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(serverProvider).build();
final SslContext sslClientCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).sslProvider(clientProvider).build();
EventLoopGroup group = new NioEventLoopGroup();
Channel sc = null;
Channel cc = null;
try {
final Promise<Void> donePromise = group.next().newPromise();
final int expectedBytes = 469 + 1024 + 1024;
sc = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(sslServerCtx.newHandler(ch.alloc()));
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
if (evt instanceof SslHandshakeCompletionEvent) {
SslHandshakeCompletionEvent sslEvt = (SslHandshakeCompletionEvent) evt;
if (sslEvt.isSuccess()) {
final ByteBuf input = ctx.alloc().buffer();
input.writeBytes(new byte[expectedBytes]);
CompositeByteBuf content = ctx.alloc().compositeBuffer();
content.addComponent(true, input.readRetainedSlice(469));
content.addComponent(true, input.readRetainedSlice(1024));
content.addComponent(true, input.readRetainedSlice(1024));
ctx.writeAndFlush(content).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
input.release();
}
});
} else {
donePromise.tryFailure(sslEvt.cause());
}
}
ctx.fireUserEventTriggered(evt);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
donePromise.tryFailure(cause);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
donePromise.tryFailure(new IllegalStateException("server closed"));
}
});
}
}).bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
cc = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(sslClientCtx.newHandler(ch.alloc()));
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
private int bytesSeen;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof ByteBuf) {
bytesSeen += ((ByteBuf) msg).readableBytes();
if (bytesSeen == expectedBytes) {
donePromise.trySuccess(null);
}
}
ReferenceCountUtil.release(msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
donePromise.tryFailure(cause);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
donePromise.tryFailure(new IllegalStateException("client closed"));
}
});
}
}).connect(sc.localAddress()).syncUninterruptibly().channel();
donePromise.get();
} finally {
if (cc != null) {
cc.close().syncUninterruptibly();
}
if (sc != null) {
sc.close().syncUninterruptibly();
}
group.shutdownGracefully();
ReferenceCountUtil.release(sslServerCtx);
ReferenceCountUtil.release(sslClientCtx);
ssc.delete();
}
}
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 intellij-community by JetBrains.
the class Decoder method getBufferIfSufficient.
@Nullable
protected final ByteBuf getBufferIfSufficient(@NotNull ByteBuf input, int requiredLength, @NotNull ChannelHandlerContext context) {
if (!input.isReadable()) {
return null;
}
if (cumulation == null) {
if (input.readableBytes() < requiredLength) {
cumulation = input;
input.retain();
input.touch();
return null;
} else {
return input;
}
} else {
int currentAccumulatedByteCount = cumulation.readableBytes();
if ((currentAccumulatedByteCount + input.readableBytes()) < requiredLength) {
CompositeByteBuf compositeByteBuf;
if ((cumulation instanceof CompositeByteBuf)) {
compositeByteBuf = (CompositeByteBuf) cumulation;
} else {
compositeByteBuf = context.alloc().compositeBuffer(DEFAULT_MAX_COMPOSITE_BUFFER_COMPONENTS);
compositeByteBuf.addComponent(cumulation);
cumulation = compositeByteBuf;
}
compositeByteBuf.addComponent(input);
input.retain();
input.touch();
return null;
} else {
CompositeByteBuf buffer;
if (cumulation instanceof CompositeByteBuf) {
buffer = (CompositeByteBuf) cumulation;
buffer.addComponent(input);
} else {
// may be it will be used by client to cumulate something - don't set artificial restriction (2)
buffer = context.alloc().compositeBuffer(DEFAULT_MAX_COMPOSITE_BUFFER_COMPONENTS);
buffer.addComponents(cumulation, input);
}
// we don't set writerIndex on addComponent, it is clear to set it to requiredLength here
buffer.writerIndex(requiredLength);
input.skipBytes(requiredLength - currentAccumulatedByteCount);
input.retain();
input.touch();
cumulation = null;
return buffer;
}
}
}
use of io.netty.buffer.CompositeByteBuf in project drill by apache.
the class RpcEncoder method encode.
@Override
protected void encode(ChannelHandlerContext ctx, OutboundRpcMessage msg, List<Object> out) throws Exception {
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("Rpc Encoder called with msg {}", msg);
}
if (!ctx.channel().isOpen()) {
//output.add(ctx.alloc().buffer(0));
logger.debug("Channel closed, skipping encode.");
msg.release();
return;
}
try {
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("Encoding outbound message {}", msg);
}
// first we build the RpcHeader
RpcHeader header = //
RpcHeader.newBuilder().setMode(//
msg.mode).setCoordinationId(//
msg.coordinationId).setRpcType(msg.rpcType).build();
// figure out the full length
int headerLength = header.getSerializedSize();
int protoBodyLength = msg.pBody.getSerializedSize();
int rawBodyLength = msg.getRawBodySize();
//
int fullLength = //
HEADER_TAG_LENGTH + getRawVarintSize(headerLength) + headerLength + PROTOBUF_BODY_TAG_LENGTH + getRawVarintSize(protoBodyLength) + //
protoBodyLength;
if (rawBodyLength > 0) {
fullLength += (RAW_BODY_TAG_LENGTH + getRawVarintSize(rawBodyLength) + rawBodyLength);
}
ByteBuf buf = ctx.alloc().buffer();
OutputStream os = new ByteBufOutputStream(buf);
CodedOutputStream cos = CodedOutputStream.newInstance(os);
// write full length first (this is length delimited stream).
cos.writeRawVarint32(fullLength);
// write header
cos.writeRawVarint32(HEADER_TAG);
cos.writeRawVarint32(headerLength);
header.writeTo(cos);
// write protobuf body length and body
cos.writeRawVarint32(PROTOBUF_BODY_TAG);
cos.writeRawVarint32(protoBodyLength);
msg.pBody.writeTo(cos);
// if exists, write data body and tag.
if (msg.getRawBodySize() > 0) {
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("Writing raw body of size {}", msg.getRawBodySize());
}
cos.writeRawVarint32(RAW_BODY_TAG);
cos.writeRawVarint32(rawBodyLength);
// need to flush so that dbody goes after if cos is caching.
cos.flush();
final CompositeByteBuf cbb = ctx.alloc().compositeBuffer(msg.dBodies.length + 1);
cbb.addComponent(buf);
int bufLength = buf.readableBytes();
for (ByteBuf b : msg.dBodies) {
cbb.addComponent(b);
bufLength += b.readableBytes();
}
cbb.writerIndex(bufLength);
out.add(cbb);
} else {
cos.flush();
out.add(buf);
}
if (RpcConstants.SOME_DEBUGGING) {
logger.debug("Wrote message length {}:{} bytes (head:body). Message: " + msg, getRawVarintSize(fullLength), fullLength);
}
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("Sent message. Ending writer index was {}.", buf.writerIndex());
}
} finally {
// make sure to release Rpc Messages underlying byte buffers.
//msg.release();
}
}
Aggregations