package co.dc.aptiv.mina;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.*;

public class MessageCodecFactory implements ProtocolCodecFactory {
	/**
	 * 发送加密
	 */
	private final ProtocolEncoderAdapter encoder;
	/**
	 * 接收解密
	 */
	private final ProtocolDecoderAdapter decoder;

	public MessageCodecFactory(boolean isEncoder, boolean isDecoder) {
		if (isEncoder) {
			encoder = new DataEncoderEx();
		} else {
			encoder = new ProtocolEncoderAdapter() {
				public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
					out.write(message);
				}
			};
		}
		if (isDecoder) {
			decoder = new DataDecoderEx();
		} else {
			decoder = new CumulativeProtocolDecoder() {
				@Override
				protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
					out.write(in);
					return true;
				}
			};
		}
	}
	
	

	public ProtocolEncoder getEncoder(IoSession session) throws Exception {
		return encoder;
	}

	public ProtocolDecoder getDecoder(IoSession session) throws Exception {
		return decoder;
	}
	
	
	public static void main(String[] args) {
		byte b = 0x50;
		System.out.println(b);
	}

}
