参见 Netty的 LengthFieldBasedFrameDecoder
public class TestLengthDecoder {
public static void main(String[] args) {
EmbeddedChannel channel = new EmbeddedChannel(
new LengthFieldBasedFrameDecoder(1024,0,4,0,4),
new LoggingHandler(LogLevel.DEBUG),
new ChannelInboundHandlerAdapter(){
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
System.out.println(buf.toString(Charset.defaultCharset()));
}
}
);
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
buf.writeInt(12);
buf.writeBytes("hello, world".getBytes());
buf.writeInt(6);
buf.writeBytes("你好".getBytes());
channel.writeInbound(buf);
}
}