首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

mina解包跟发包

2013-03-25 
mina解包和发包解包:发包:public void encode(IoSession session, Object message,ProtocolEncoderOutput

mina解包和发包
解包:


发包:

public void encode(IoSession session, Object message,ProtocolEncoderOutput out) throws Exception  {  //System.out.println("**********          AMF3Encoder      *************");  if(message instanceof String)  {   byte[] bytes = ((String) message).getBytes("UTF-8");   IoBuffer buffer = IoBuffer.allocate(bytes.length+1);   buffer.put(bytes);   buffer.put((byte) 0x0);//要在末尾写入0,表示一个流信息的结束   buffer.flip();   out.write(buffer);   buffer.free();// 清空?   buffer.clear();  }  else  {   IoBuffer buffer;      ByteArrayOutputStream stream = new ByteArrayOutputStream();   amf3out.setOutputStream(stream);   amf3out.writeObject(message);   amf3out.flush();   byte bytes[] = stream.toByteArray();     //byte bytes[] = compress(session,stream.toByteArray());     buffer = IoBuffer.allocate(bytes.length+4,false);//创建缓存   buffer.putInt(bytes.length);//总长度   //System.out.println(bytes.length);   buffer.put(bytes);//内容   buffer.flip();   out.write(buffer);   buffer.free();// 清空?  }  }

热点排行