mina 字节数组编解码器的写法 II

I 里面的写法不够严谨,这也是我之前说它简陋的主要原因,下面来个更加严谨、完整一点儿的:

ByteArrayEncoder.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;
	}
}


原文地址:https://www.cnblogs.com/jiangu66/p/3206506.html