Netty MessageDecoder decode详解
来源:互联网 发布:linux发行版排行 2017 编辑:程序博客网 时间:2024/06/06 05:44
首先看下 Netty MessageDecoder 类:
public class MessageDecoder extends FrameDecoder { @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { if (buffer.readableBytes() < 4) { return null;//(1) } int dataLength = buffer.getInt(buffer.readerIndex()); if (buffer.readableBytes() < dataLength + 4) { return null;//(2) } buffer.skipBytes(4);//(3) byte[] decoded = new byte[dataLength]; buffer.readBytes(decoded); String msg = new String(decoded);//(4) return msg; }}
MessageDecoder继承自FrameDecoder,FrameDecoder是Netty codec包中的辅助类,它是个ChannelUpstreamHandler,decode方法是FrameDecoder子类需要实现的。在上面的代码中,有:
(1)检查ChannelBuffer中的字节数,如果ChannelBuffer可读的字节数少于4,则返回null等待下次读事件。
(2)继续检查ChannelBuffer中的字节数,如果ChannelBuffer可读的字节数少于dataLength + 4,则返回null等待下次读事件。
(3)越过dataLength的字节。
(4)构造解码的字符串返回。
0 0
- Netty MessageDecoder decode详解
- Netty pipeLine decode的顺序
- decode详解
- Decode详解
- Decode详解
- Netty详解
- netty 详解
- Oracle Decode函数详解
- Oracle Decode函数详解
- Oracle Decode函数详解
- Netty中ChannelBuffer详解
- Netty线程模型详解
- Netty框架启动详解
- Netty线程模型详解
- Netty线程模型详解
- netty 服务详解
- 【Netty基础】protobuf详解
- Netty网络编程详解
- gic的逻辑划分:distributor与cpu interface
- win7下安装Ubuntu双系统
- poj 1287 Networking
- 数字在排序数组中出现的次数
- 网络流
- Netty MessageDecoder decode详解
- 放学回家发个邮件共负盈亏附件一警方已就发邮件
- HDU4551 猜生日问题
- 数据大观园(一)
- Netty 中为什么不需要 flip方法
- MySql 5.6.20乱码问题的解决
- Arrays.copyof(···)与System.arraycopy(···)区别
- 如何让微信好友“爱”上你
- jQuery Easy UI Panel(面板)组件