package demo2;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
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 MyTextLineCodecDecoderII implements ProtocolDecoder {
// 首先定义Charset字符集编码格式
private Charset charset;
// 定义文本分隔符
private String delimiter;
// 文本分隔符匹配的变量
private IoBuffer delimBuf;
// 定义常量值,作为每个IoSession中保存解码内容的key值
public static String CONTEXT = MyTextLineCodecDecoderII.class.getName()
+ ".context";
/**
* 构造函数,必须制定charset和delimiter Constructor, you must specify the Charset and
* text delimiter
*
* @param charset
* 字符集
* @param delimiter
* Text separator type is string
*/
public MyTextLineCodecDecoderII(Charset charset, String delimiter) {
this.charset = charset;
this.delimiter = delimiter;
}
// 获得IoBuffer的实例对象,allcoate静态方法设置初始容量为100,然后Set AutoExpand 自动增长为True 自动扩容
// 用户存储接收到的数据
IoBuffer buf = IoBuffer.allocate(100).setAutoExpand(true);
/**
* decode方法的参数IoBuffer是建立连接后接收数据的字节数组; 我们不断的从它里面读数据, 把上面累加的所有数据转换为一个字符串,输出!
*/
@Override
public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
throws Exception {
// 获得存储对象内部类定义的
Context ctx = getContext(session);
// 初始化文本分隔符和编码格式
if (null == delimiter || delimiter.equals(""))
delimiter = "\r\n";
if (charset == null)
charset = Charset.forName("utf-8");
decodeNormal(ctx, in, out);
}
public void decodeNormal(Context ctx, IoBuffer in, ProtocolDecoderOutput out)
throws CharacterCodingException {
//取出未完成任务中已经匹配的文本行符的个数
int matchCount = ctx.getMatchCount();
//设置文本换行符的IoBuffer变量
if(delimBuf == null ){
IoBuffer tmp = IoBuffer.allocate(2).setAutoExpand(true);
tmp.putString(delimiter, charset.newEncoder());
tmp.flip();
delimBuf = tmp ;
}
//解码的IoBuffer中数据的原始信息
//position=0 指向第一个元素为0
int oldPos = in.position();
//oldLimit=-1 指向第一个元素之前的即-1
int oldLimit = in.limit();
while(in.hasRemaining()){ //变量解码的Iobuffer
byte b = in.get(); //读取一个字节
// 匹配低matchCount位换行符成功
byte c = delimBuf.get(matchCount);
out.write(b);
out.write(c);
if(c == b){
matchCount++;
//当前匹配到的字节个数与文本中的字节个数相同匹配成功。limit方法是获取delimbuf的总字节数
if(matchCount==delimBuf.limit()){
// 获得当前匹配到的position(position前素有数据有效)
int pos = in.position();
//设置缓冲区的限制。如果位置大于新的限制值,则它被设置为新的限制。
//如果该标记被定义和大于新的限制值,则它会被丢弃。
in.limit(pos);
//回到原始位置
in.position(oldPos);
ctx.append(in); //追加到Context对象未完成数据后面
in.limit(oldLimit); //in中匹配结束后剩余数据
in.position(pos); // 传输过来的iobuffer对象position位置
IoBuffer buf = ctx.getBuf();
//limit 指向position的位置,mark和position还原为初始值
buf.flip();
//去掉匹配数据文本换行符
buf.limit(buf.limit()-matchCount); //去掉匹配数据文本换行符 把\r\n去掉
try{
//输出解码
out.write(buf.getString(ctx.getDecoder()));
}finally{
buf.clear();//释放缓存空间
}
oldPos = pos;
matchCount = 0 ;
}
}else {
//如果matchCount==0 则继续匹配
//如果matchCount>0,说明没有匹配到文本换行符的中的前一个匹配
//成功字节的下一个字节,
//跳转到匹配失败字符处,并置matchCount=0 继续匹配
int intPosition = in.position();
in.position(intPosition-matchCount);
matchCount = 0 ; //匹配成功后matchCount置空
}
}
//把in中未解码内容放回buf中
in.position(oldPos);
ctx.append(in);
ctx.setMatchCount(matchCount);
}
/**
* 获得存储对象
* // 内部类,保存IoSession解码时未完成的任务
* @param session
* @return
*/
private Context getContext(IoSession session) {
Context ctx = (Context) session.getAttribute(CONTEXT);
if (ctx == null) {
ctx = new Context();
session.setAttribute(CONTEXT, ctx);
}
return ctx;
}
private void decodeAuto(Context ctx, IoBuffer in, ProtocolDecoderOutput out)
throws CharacterCodingException {
boolean mark = false;
while (in.hasRemaining()) {
byte b = in.get();
switch (b) {
case '\r':
break;
case '\n':
mark = true;
break; // 跳出switch
default:
ctx.getBuf().put(b);
}
if (mark) {
IoBuffer t_buf = ctx.getBuf();
t_buf.flip();
try {
out.write(t_buf.getString(charset.newDecoder()));
} finally {
t_buf.clear();
}
}
}
}
/**
* Decoding container Temporary storage buffer object When executing dispose
* method to remove the storage objects in session
*
* @author zhangf
*
*/
private class Context {
//字符集解码器
private CharsetDecoder decoder;
//保存真实解码内容
private IoBuffer buf;
//匹配到文本换行符个数
private int matchCount = 0;
//构造函数
private Context (){
decoder = charset.newDecoder();
buf = IoBuffer.allocate(100).setAutoExpand(true);
}
// 重置
public void reset() {
matchCount = 0;
decoder.reset();
}
// 追加数据
public void append(IoBuffer in) {
getBuf().put(in);
}
public IoBuffer getBuf() {
return buf;
}
public CharsetDecoder getDecoder() {
return decoder;
}
public int getMatchCount() {
return matchCount;
}
public void setMatchCount(int matchCount) {
this.matchCount = matchCount;
}
}
public void dispose(IoSession session) throws Exception {
/*Context ctx = (Context) session.getAttribute(CONTEXT);
if (ctx != null) {
session.removeAttribute(CONTEXT);
}*/
}
@Override
public void finishDecode(IoSession arg0, ProtocolDecoderOutput arg1)
throws Exception {
// TODO Auto-generated method stub
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
testmina.rar_mina
共64个文件
class:30个
java:29个
log:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 90 浏览量
2022-09-24
06:40:09
上传
评论
收藏 61KB RAR 举报
温馨提示
java Mina通讯源码,测试密码通讯的源文件。
资源推荐
资源详情
资源评论
收起资源包目录
testmina.rar (64个子文件)
testmina
log
minademos.log 434B
bin
Test.class 512B
Demo1ClientHandler.class 1KB
Demo1ServerHandler.class 3KB
zf
MyTextLineCodecFactory.class 895B
Demo01Server.class 2KB
MyTextLineCodecDecoder.class 3KB
MyTextLineCodecEncoder.class 2KB
Demo01ServerHandler.class 3KB
demo2
Demo01Server.class 3KB
MyTextLineCodecDecoderII$Context.class 2KB
MyTextLineCodecEncoder.class 2KB
Demo01ServerHandler.class 3KB
MyTextLineCodecFactoryII.class 1KB
MyTextLineCodecDecoderII.class 5KB
Demo1Server.class 3KB
TestLog4j.class 1003B
MinaClient01.class 3KB
demo3
MyMessageEncoder.class 4KB
AbstrMessage.class 375B
EventDto.class 2KB
Demo2ClientHandler.class 3KB
Demo2ServerHandler.class 4KB
ChannelInfoResponse.class 2KB
ChannelInfoRequest.class 2KB
TestClient02.class 3KB
MyMessageCodecFactory.class 1KB
TestServer02.class 3KB
MyMessageDecoder.class 5KB
MarkAndPositionAndLimit.class 1KB
demo4
AbstrMessage.class 375B
log4j.properties 585B
.settings
org.eclipse.jdt.core.prefs 629B
src
Demo1ClientHandler.java 620B
MinaClient01.java 2KB
zf
Demo01ServerHandler.java 2KB
MyTextLineCodecFactory.java 651B
MyTextLineCodecDecoder.java 2KB
MyTextLineCodecEncoder.java 1KB
Demo01Server.java 2KB
demo2
Demo01ServerHandler.java 2KB
MyTextLineCodecFactoryII.java 933B
MyTextLineCodecEncoder.java 1KB
MyTextLineCodecDecoderII.java 6KB
Demo01Server.java 2KB
demo3
Demo2ServerHandler.java 2KB
ChannelInfoRequest.java 1KB
EventDto.java 2KB
MyMessageEncoder.java 3KB
MyMessageDecoder.java 4KB
AbstrMessage.java 269B
ChannelInfoResponse.java 1KB
TestServer02.java 2KB
MyMessageCodecFactory.java 642B
Demo2ClientHandler.java 1KB
TestClient02.java 2KB
demo4
AbstrMessage.java 269B
TestLog4j.java 534B
MarkAndPositionAndLimit.java 1KB
Test.java 134B
Demo1Server.java 2KB
Demo1ServerHandler.java 1KB
.project 384B
.classpath 373B
共 64 条
- 1
资源评论
林当时
- 粉丝: 100
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功