| New file | 
|  |  |  | 
|---|
|  |  |  | package com.dy.pipIrrMwTestRtu.tcpClient; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.dy.common.util.ByteUtil; | 
|---|
|  |  |  | import org.apache.logging.log4j.LogManager; | 
|---|
|  |  |  | import org.apache.logging.log4j.Logger; | 
|---|
|  |  |  | 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 { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static final Logger log = LogManager.getLogger(LocalEncoder.class) ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 对数据进行编码,以备网络传输 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | 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()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|