TextLineCodecFactory笔记

Mina的TextLineCodecFactory将字符串编码为字节流,将字节流解码为字符串,下面是使用中遇到的两个问题。 TextLineCodecFactory改变了message的类型 acceptor上挂了textCodec后handler的messageReceived方法中的session是String型(用"message.getClass().getName()"查看),

[INFO] [2012-04-05 16:18:54,317] [com.leechau.mina.echoServer.EchoProtocolHandler] - [Received : hi(java.lang.String)]

否则是IoBuffer:

[INFO] [2012-04-05 15:23:36,760] [com.leechau.mina.echoServer.EchoProtocolHandler] - [message type is: org.apache.mina.core.buffer.SimpleBufferAllocator$SimpleBuffer]

因此挂上TextLineCodecFactory后在handler的messageReceived方法中message参数可直接强转为String;

不能正确解码换行符问题及解决办法 连接Echo Server后有时会出现下面的现象:

hi{enter}

send:

hi

造成这一现象的原因是下面的配置方法使TextLineCodecFactory使用了默认的编解码行分隔符(encoder/decoder line delimeter),即用Unix LineDelimeter(/n)作为编码行分隔符,用AUTO LineDelimeter(\r或者\n,详见Mina API doc对LineDelimeter类的说明)作为解码行分隔符:

acceptor.getFilterChain().addLast("textCodec",new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

解决这一问题的办法是指定编解码行分隔符:

acceptor.getFilterChain().addLast("textCodec",

new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"),

LineDelimiter.DEFAULT.getValue(), LineDelimiter.DEFAULT.getValue())));

效果如下:

hi{enter} send: hi

如果不希望每次session.write(msg)后编码器自动加上回车符,需要将编码行分隔符指定为NUL:

acceptor.getFilterChain().addLast(" textCodec",

new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"),

LineDelimiter.NUL.getValue(), LineDelimiter.DEFAULT.getValue())));

效果如下:

hi{enter} send: hi

源代码 Main.java

package com.leechau.mina.echoServer; import java.net.InetSocketAddress; import java.nio.charset.Charset; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.textline.TextLineCodecFactory; import org.apache.mina.filter.logging.LoggingFilter; import org.apache.mina.transport.socket.SocketAcceptor; import org.apache.mina.transport.socket.nio.NioSocketAcceptor; public class Main { private static final int PORT = 8293; public static void main(String[] args) throws Exception { SocketAcceptor acceptor = new NioSocketAcceptor(); acceptor.getFilterChain().addLast("logger", new LoggingFilter()); acceptor.getFilterChain().addLast("textCodec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"), LineDelimiter.NUL.getValue() , LineDelimiter.DEFAULT.getValue()))); acceptor.setHandler(new EchoProtocolHandler()); acceptor.bind(new InetSocketAddress(PORT)); System.out.println("Listening on port " + PORT); } }

EchoProtocolHandler.java

package com.leechau.mina.echoServer; import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IoSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class EchoProtocolHandler extends IoHandlerAdapter { private static final Logger LOGGER = LoggerFactory .getLogger(EchoProtocolHandler.class); @Override public void messageReceived(IoSession session, Object message) throws Exception { LOGGER.info("Received : " + message + "(" + message.getClass().getName() + ")"); session.write("send: "); session.write(message); } }

posted on 2014-03-18 08:15  leechau  阅读(1707)  评论(0编辑  收藏  举报

导航