自定义netty的解码器decode读取数据报错,版本:
<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.77.Final</version> </dependency>
报错的是这行代码:
// 读取消息内容 byte[] content = in.readBytes(in.readableBytes()).array();
由于ByteBuf是使用了DirectBuffer,所以不支持array()的操作
改为:
byte[] data = new byte[contentLength]; in.readBytes(data);
程序正常运行,OK!