| New file | 
 |  |  | 
 |  |  | package com.dy.testClient.tcpClient; | 
 |  |  |  | 
 |  |  | import org.apache.mina.core.buffer.IoBuffer; | 
 |  |  | import org.apache.mina.core.session.IoSession; | 
 |  |  | import org.apache.mina.filter.codec.ProtocolEncoderAdapter; | 
 |  |  | import org.apache.mina.filter.codec.ProtocolEncoderOutput; | 
 |  |  |  | 
 |  |  | import java.io.IOException; | 
 |  |  |  | 
 |  |  | public class LocalEncoder extends ProtocolEncoderAdapter { | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 对数据进行编码,以备网络传输 | 
 |  |  |      */ | 
 |  |  |     public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws IOException, Exception{ | 
 |  |  |         byte[] data = (byte[])message; | 
 |  |  |         //log.info("下行数据:" + ByteUtil.bytes2Hex(data, true)); | 
 |  |  |  | 
 |  |  |         out.write(IoBuffer.allocate((data==null?0:data.length), false).put(data).flip()); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |