mina 字节数组编解码器的写法
2015-11-10 13:54
162 查看
ByteArrayEncoder.java
ByteArrayDecoder.java
ByteArrayCodecFactory.java
NumberUtil.java
package org.bruce.mina.cpp.codec; import org.apache.mina.core.buffer.IoBuffer; import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.ProtocolEncoderAdapter; import org.apache.mina.filter.codec.ProtocolEncoderOutput; import org.bruce.mina.cpp.util.NumberUtil; /** * @author BruceYang * 编写编码器的注意事项: * 1、 mina 为 IoSession 写队列里的每个对象调用 ProtocolEncode.encode 方法。 * 因为业务处理器里写出的都是与编码器对应高层对象,所以可以直接进行类型转换。 * 2、从 jvm 堆分配 IoBuffer,最好避免使用直接缓存,因为堆缓存一般有更好的性能。 * 3、开发人员不需要释放缓存, mina 会释放。 * 4、在 dispose 方法里可以释放编码所需的资源。 */ public class ByteArrayEncoder extends ProtocolEncoderAdapter { @Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { // TODO Auto-generated method stub byte[] dataBytes = (byte[])message; byte[] sizeBytes = NumberUtil.int2bytes(dataBytes.length); IoBuffer buffer = IoBuffer.allocate(256); buffer.setAutoExpand(true); buffer.put(sizeBytes); buffer.put(dataBytes); buffer.flip(); out.write(buffer); out.flush(); buffer.free(); } }
ByteArrayDecoder.java
package org.bruce.mina.cpp.codec; import org.apache.mina.core.buffer.IoBuffer; import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.CumulativeProtocolDecoder; import org.apache.mina.filter.codec.ProtocolDecoderOutput; import org.bruce.mina.cpp.util.NumberUtil; /** * @author BruceYang * 字节数组解码器 */ public class ByteArrayDecoder extends CumulativeProtocolDecoder { public boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { // TODO Auto-generated method stub if (in.remaining() > 0) { // 有数据时,读取 4 字节判断消息长度 byte[] sizeBytes = new byte[4]; // 标记当前位置,以便 reset in.mark(); // 读取钱 4 个字节 in.get(sizeBytes); // NumberUtil 是自己写的一个 int 转 byte[] 的工具类 int size = NumberUtil.bytes2int(sizeBytes); if (size > in.remaining()) { // 如果消息内容的长度不够,则重置(相当于不读取 size),返回 false in.reset(); // 接收新数据,以拼凑成完整的数据~ return false; } else { byte[] dataBytes = new byte[size]; in.get(dataBytes, 0, size); out.write(dataBytes); if (in.remaining() > 0) { // 如果读取内容后还粘了包,就让父类把剩下的数据再给解析一次~ return true; } } } // 处理成功,让父类进行接收下个包 return false; } }
ByteArrayCodecFactory.java
package org.bruce.mina.cpp.codec; import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.ProtocolCodecFactory; import org.apache.mina.filter.codec.ProtocolDecoder; import org.apache.mina.filter.codec.ProtocolEncoder; /** * @author BruceYang * 字节数组编解码工厂 */ public class ByteArrayCodecFactory implements ProtocolCodecFactory { private ByteArrayDecoder decoder; private ByteArrayEncoder encoder; public ByteArrayCodecFactory() { encoder = new ByteArrayEncoder(); decoder = new ByteArrayDecoder(); } @Override public ProtocolDecoder getDecoder(IoSession session) throws Exception { return decoder; } @Override public ProtocolEncoder getEncoder(IoSession session) throws Exception { return encoder; } }
NumberUtil.java
package org.bruce.mina.cpp.util; /** * @author yang3wei * int、byte[] 相互转换的工具类~ */ public class NumberUtil { /** * 将整型转换为字节数组~ * @param integer * @return */ public static byte[] int2bytes(int integer) { byte[] bytes = new byte[4]; bytes[0] = (byte) (integer & 0xff); // 最低位 bytes[1] = (byte) ((integer >> 8) & 0xff); // 次低位 bytes[2] = (byte) ((integer >> 16) & 0xff); // 次高位 bytes[3] = (byte) (integer >>> 24); // 最高位,无符号右移。 return bytes; } /** * 将字节数组转换为整型~ * @param bytes * @return */ public static int bytes2int(byte[] bytes) { // 一个 byte 数据左移 24 位变成 0x??000000,再右移 8 位变成 0x00??0000(| 表示按位或) int integer = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00) | ((bytes[2] << 24) >>> 8) | (bytes[3] << 24); return integer; } }
相关文章推荐
- 分布式服务的Trace——Google Dapper & Twitter Zipkin
- linux查看本机IP、gateway、dns
- C++轻量级日志类CLogger的使用(更新)
- 测试oracle存储过程,光标,连接等的java代码
- [LeetCode]题解(python):045-Jump Game II
- Dire Wolf ---hdu5115(区间dp)
- 25个 Git 进阶技巧
- Python核心数据类型——布尔型
- 深入理解PHP内核(一)
- Linux下*.tar.gz文件解压缩命令
- liftweb整合ckfinder进行文件上传与管理
- 如何安装Snappy Ubuntu到树莓派(RaspBerry PI)上
- HTML5初学者路线图
- opencv3.0.0 for android .判断两张图片是否一致
- 存储过程例子(1)
- 光学基础知识:白光、颜色混合、RGB、色彩空间
- 男人必须有的十大奢侈品
- 部署Squid透明代理
- keepalived 配置文件解析
- 玩转树莓派之环境搭建 --- 系统准备