zoukankan      html  css  js  c++  java
  • 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( 或者 ,详见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); } }

  • 相关阅读:
    JAVA 8的新特性
    JAVA中map的分类和各自的特性
    设计模式之工厂方法模式(附实例代码下载)
    为什么重写equals还要重写hashcode??
    C# static的用法详解
    1-RadioButton控件的用法
    C#三种常用的读取XML文件的方法
    VS2017 中安装SVN
    pip安装selenium时,报错“You are using pip version 10.0.1, however version 18.0 is available.”的问题
    问题:unknown error: call function result missing 'value' 解决方法
  • 原文地址:https://www.cnblogs.com/darkmatter/p/3606837.html
Copyright © 2011-2022 走看看