package com.persagy.iottransfer.communication.mina.codec; import com.persagy.iottransfer.communication.entity.Packet; import com.persagy.iottransfer.communication.util.MyDecoder; import org.apache.mina.core.buffer.IoBuffer; import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.CumulativeProtocolDecoder; import org.apache.mina.filter.codec.ProtocolDecoderOutput; import java.util.HashMap; import java.util.List; import java.util.Map; public class MinaPacketDecoder extends CumulativeProtocolDecoder { String ip; int port; String encoding; String aes_password; boolean compress = false; boolean separate = false; boolean separateBytes = false; byte prefix; byte suffix; byte[] prefixBytes; byte[] suffixBytes; MyDecoder decoder; Map> tmpByteListMap = new HashMap>(); public MinaPacketDecoder(String ip, int port, String encoding, String aes_password, boolean compress) { this.ip = ip; this.port = port; this.encoding = encoding; this.aes_password = aes_password; this.compress = compress; this.separate = false; this.decoder = new MyDecoder(this.ip, this.port, this.encoding, this.aes_password, this.compress, this.separate, this.separateBytes, this.prefix, this.suffix, this.prefixBytes, this.suffixBytes); } public MinaPacketDecoder(String ip, int port, String encoding, String aes_password, boolean compress, byte prefix, byte suffix) { this.ip = ip; this.port = port; this.encoding = encoding; this.aes_password = aes_password; this.compress = compress; this.separate = true; this.separateBytes = false; this.prefix = prefix; this.suffix = suffix; this.decoder = new MyDecoder(this.ip, this.port, this.encoding, this.aes_password, this.compress, this.separate, this.separateBytes, this.prefix, this.suffix, this.prefixBytes, this.suffixBytes); } public MinaPacketDecoder(String ip, int port, String encoding, String aes_password, boolean compress, byte[] prefixBytes, byte[] suffixBytes) { this.ip = ip; this.port = port; this.encoding = encoding; this.aes_password = aes_password; this.compress = compress; this.separate = true; this.separateBytes = true; this.prefixBytes = prefixBytes; this.suffixBytes = suffixBytes; this.decoder = new MyDecoder(this.ip, this.port, this.encoding, this.aes_password, this.compress, this.separate, this.separateBytes, this.prefix, this.suffix, this.prefixBytes, this.suffixBytes); } public boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { int remainingLength = in.remaining(); byte[] remainingBytes = new byte[remainingLength]; for (int i = 0; i < remainingLength; i++) { remainingBytes[i] = in.get(); } String remoteAddress = session.getRemoteAddress().toString(); List packetList = this.decoder.decode(remoteAddress, remainingBytes); for (int i = 0; i < packetList.size(); i++) { Packet packet = packetList.get(i); out.write(packet); } return true; } }