Mina使用IoBuffer读取字节的问题
问题描述:
使用telnet localhost 3005
输入123456
在console里显示:
服务端与客户端创建连接...
服务端与客户端连接打开...
b:49
b:50
b:51
b:52
b:53
b:54
rrrr
nnnnn
Msg:
服务端接收到的数据为:
Encode:Fri Apr 06 15:57:03 CST 2012
服务端发送信息成功...
为什么Msg为空呢?
这个是服务器端:
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class Demo1Server {
private static int PORT = 3005;
public static void main(String[] args) {
IoAcceptor acceptor = null;
try {
// 创建一个非阻塞的server端的Socket
acceptor = new NioSocketAcceptor();
// 设置过滤器(使用Mina提供的文本换行符编解码器)
//acceptor.getFilterChain().addLast(
//"codec",
//new ProtocolCodecFilter(new TextLineCodecFactory(Charset
//.forName("UTF-8"),
//LineDelimiter.WINDOWS.getValue(),
//LineDelimiter.WINDOWS.getValue())));
acceptor.getFilterChain().addLast(
"codec",
new ProtocolCodecFilter(new MyTextLineCodecFactory()));
// 设置读取数据的缓冲区大小
acceptor.getSessionConfig().setReadBufferSize(2048);
// 读写通道10秒内无操作进入空闲状态
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
// 绑定逻辑处理器
acceptor.setHandler(new Demo1ServerHandler());
// 绑定端口
acceptor.bind(new InetSocketAddress(PORT));
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.nio.charset.CharacterCodingException;
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.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
public class MyTextLineCodecDecoder implements ProtocolDecoder {
private Charset charset = Charset.forName("UTF-8");
IoBuffer buf = IoBuffer.allocate(100).setAutoExpand(true);
public static byte [] ioBufferToByte(Object message)
{
if (!(message instanceof IoBuffer))
{
return null;
}
IoBuffer ioBuffer = (IoBuffer)message;
byte[] b = new byte[ioBuffer.limit()];
ioBuffer.get(b);
return b;
}
@Override
public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
throws Exception {
// TODO Auto-generated method stub
while (in.hasRemaining()) {
byte b = in.get();
switch (b){
case '\r':
System.out.println("rrrr");
break;
case '\n':
System.out.println("nnnnn");
buf.flip();
String msg = buf.getString(charset.newDecoder());
System.out.println("Msg:"+msg);
out.write(msg);
break;
default:
System.out.println("b:"+b);
buf.put(b);
}
}
}
@Override
public void dispose(IoSession arg0) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void finishDecode(IoSession arg0, ProtocolDecoderOutput arg1)
throws Exception {
// TODO Auto-generated method stub
}
}
import java.util.Date;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
public class Demo1ServerHandler extends IoHandlerAdapter {
@Override
public void sessionCreated(IoSession session) throws Exception {
System.out.println("服务端与客户端创建连接...");
}
@Override
public void sessionOpened(IoSession session) throws Exception {
System.out.println("服务端与客户端连接打开...");
}
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
String msg = message.toString();
System.out.println("服务端接收到的数据为:" + msg);
if ("bye".equals(msg)) { // 服务端断开连接的条件
session.close(true);
}
Date date = new Date();
session.write(date);
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
System.out.println("服务端发送信息成功...");
}
@Override
public void sessionClosed(IoSession session) throws Exception {
}
@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
System.out.println("服务端进入空闲状态...");
}
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
System.out.println("服务端发送异常...");
}
}