Netty中所有的解码器都是基于ByteToMessageDecoder来实现的,他的实现原理如下:
1、累加字节流
2、调用子类的decode方法进行解析
3、将解析得到ByteBuf向下传播
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//如果当前传进来的对象是ByteBuf类型的,那么就直接丢给解码器进行处理,否则向下传播
if (msg instanceof ByteBuf) {
//实例化一个list
CodecOutputList out = CodecOutputList.newInstance();
try {
//将对象强制转换成bytebuf
ByteBuf data = (ByteBuf) msg;
first = cumulation == null;
//如果是null,说明是刚开始解析,直接把二进制字符串流赋值
if (first) {
cumulation = data;
} else {
//否则将二进制字符串流进行累加
cumulation = cumulator.cumulate(ctx.alloc(), cumulation, data);
}
//调用解码器进行累加
callDecode(ctx, cumulation, out);
} catch (DecoderException e) {