协议数据
1、对输出的数据进行编码,把消息对象转换成二进制字节流
2、对收到的数据进行解码
过滤器ProtocolCodecFilter,对应的两个最重要的方法分别是
1、先对数据进行编码,再发送 filterWrite(NextFilter nextFilter, IoSession session,
WriteRequest writeRequest)
2、收到数据,进行解码 messageReceived(NextFilter nextFilter, IoSession session,
Object message)
下面在代码中解释,重点看这两个方法的实现就行啦
package org.apache.mina.filter.codec;
import java.net.SocketAddress;
import java.util.Queue;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.file.FileRegion;
import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.filterchain.IoFilterChain;
import org.apache.mina.core.future.DefaultWriteFuture;
import org.apache.mina.core.future.WriteFuture;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.write.DefaultWriteRequest;
import org.apache.mina.core.write.NothingWrittenException;
import org.apache.mina.core.write.WriteRequest;
import org.apache.mina.core.write.WriteRequestWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An {@link IoFilter} which translates binary or protocol specific data into
* message objects and vice versa using {@link ProtocolCodecFactory},
* 协议过滤器的功能是利用ProtocolCodecFactory类提供的编码器
* 把消息对象转换成二进制流或特定协议的数据
* 或者 解码器 把二进制流或特定协议的数据转换成消息对象
* {@link ProtocolEncoder}, or {@link ProtocolDecoder}.
*/
public class ProtocolCodecFilter extends IoFilterAdapter {
/** A logger for this class */
private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolCodecFilter.class);
private static final Class<?>[] EMPTY_PARAMS = new Class[0];
private static final IoBuffer EMPTY_BUFFER = IoBuffer.wrap(new byte[0]);
private final AttributeKey ENCODER = new AttributeKey(ProtocolCodecFilter.class, "encoder");
private final AttributeKey DECODER = new AttributeKey(ProtocolCodecFilter.class, "decoder");
private final AttributeKey DECODER_OUT = new AttributeKey(ProtocolCodecFilter.class, "decoderOut");
private final AttributeKey ENCODER_OUT = new AttributeKey(ProtocolCodecFilter.class, "encoderOut");
/** The factory responsible for creating the encoder and decoder
* 负责创建编码和解码器
*/
private final ProtocolCodecFactory factory;
/**
*
* Creates a new instance of ProtocolCodecFilter, associating a factory
* for the creation of the encoder and decoder.
*
* @param factory The associated factory
*/
public ProtocolCodecFilter(ProtocolCodecFactory factory) {
if (factory == null) {
throw new IllegalArgumentException("factory");
}
this.factory = factory;
}
/**
* Creates a new instance of ProtocolCodecFilter, without any factory.
* The encoder/decoder factory will be created as an inner class, using
* the two parameters (encoder and decoder).
*
* @param encoder The class responsible for encoding the message
* @param decoder The class responsible for decoding the message
*/
public ProtocolCodecFilter(final ProtocolEncoder encoder,
final ProtocolDecoder decoder) {
if (encoder == null) {
throw new IllegalArgumentException("encoder");
}
if (decoder == null) {
throw new IllegalArgumentException("decoder");
}
// Create the inner Factory based on the two parameters
this.factory = new ProtocolCodecFactory() {
public ProtocolEncoder getEncoder(IoSession session) {
return encoder;
}
public ProtocolDecoder getDecoder(