Minaカスタムテキストエンコーディング

1363 ワード

テキストエンコーディング
package com.boonya.protocol.codec.self;
import java.nio.charset.Charset;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

public class MyTextLineCodecEncoder implements ProtocolEncoder {
	private Charset charset; //  

	private String delimiter; //  

	public MyTextLineCodecEncoder(Charset charset, String delimiter) {
		this.charset = charset;
		this.delimiter = delimiter;
	}

	public void encode(IoSession session, Object message,
			ProtocolEncoderOutput out) throws Exception {
		if (delimiter == null || "".equals(delimiter)) { //  , 
			delimiter = "\r
"; } if (charset == null) { charset = Charset.forName("utf-8"); } String value = message.toString(); IoBuffer buf = IoBuffer.allocate(value.length()).setAutoExpand(true); buf.putString(value, charset.newEncoder()); // buf.putString(delimiter, charset.newEncoder()); // buf.flip(); out.write(buf); } public void dispose(IoSession session) throws Exception { } }