com.github.dreamhead.moco.internal.MocoAggregator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moco-core Show documentation
Show all versions of moco-core Show documentation
Moco is an easy setup stub framework, mainly focusing on testing and integration.
package com.github.dreamhead.moco.internal;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class MocoAggregator extends ChannelInboundHandlerAdapter {
private CompositeByteBuf bufs = ByteBufAllocator.DEFAULT.compositeBuffer();
@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
if (msg instanceof ByteBuf) {
ByteBuf buf = (ByteBuf) msg;
bufs.addComponent(buf);
bufs.writerIndex(bufs.writerIndex() + buf.writerIndex());
}
}
@Override
public void channelReadComplete(final ChannelHandlerContext ctx) throws Exception {
if (bufs.numComponents() > 0) {
ctx.fireChannelRead(bufs);
bufs = ByteBufAllocator.DEFAULT.compositeBuffer();
}
ctx.fireChannelReadComplete();
}
}