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();// 清空? } }