mina 解码器中数组溢出问题
说明程序做的对信息的加密解密传输,即下位机不断给服务器发送数据,
如下是服务器的解码代码:
package Coder;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
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;
public class CmccSipcDecoder extends CumulativeProtocolDecoder
{
private final Charset charset;
public CmccSipcDecoder(Charset charset) {
this.charset = charset;
}
@Override
protected boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
System.out.println("--------------------解码-----------------");
int limit = in.limit();
byte[] byteb = new byte[limit];
in.get(byteb);
System.out.println("**********************************解码长度:"+byteb.length);
for(int i=0;i<byteb.length;i++){
System.out.print(byteb[i]);
}
/*System.out.println("\n");
int packeg = (byteb[4]& 0xFF)*256 + (byteb[5]& 0xFF);
int packegSub = (byteb[6]& 0xFF)*256 + (byteb[7]& 0xFF);
System.out.println("包类型-字类型:"+packeg+"::"+packegSub);
*/
int packeglen1 = (byteb[8]& 0xFF)*256 + (byteb[9]& 0xFF);
System.out.println("\n***********************************packeglen1:"+packeglen1);
//int packeglen2 = (byteb[10]& 0xFF)*256 + (byteb[11]& 0xFF);
int factualLen = 0;
//证明包格式正确
if((byteb[0] & 0xFF) == 0xA8
&& (byteb[12] & 0xFF) == 0xA9
&& (byteb[13 + packeglen1 +1] & 0xFF) == 0x1A)//&& (byteb[13 + packeglen1 +1] & 0xFF) == 0x1A
{
factualLen = 15 + packeglen1;
}
byte[] bytec = new byte[factualLen];
for(int i = 0; i< factualLen; i ++)
bytec[i] = byteb[i];
out.write(bytec);
//out.write(byteb);
return true;
}
}
运行过程中会出现如下错误:(我贴出了错误的附近代码)
--------------------解码-----------------
**********************************解码长度:64
-8881510000064064-87-5-715552-12673-707053107-42118-7-97-448848-23107-28-127-41-6710778-91531091549-74105-12444-1653103-8547-991122811512533-92-92-10582-2534
***********************************packeglen1:64
2014-01-21 09:37:53,217 DEBUG IoFilterEvent - Firing a EXCEPTION_CAUGHT event for session 3
2014-01-21 09:37:53,217 INFO LoggingFilter - RECEIVED: HeapBuffer[pos=0 lim=15 cap=128: A7 B3 4B 83 A1 9C 78 3D E5 1E BF 75 F1 00 1A]
java.lang.ArrayIndexOutOfBoundsException (Hexdump: A8 51 05 01 00 00 00 00 00 40 00 40 A9 FB B9 37 34 82 49 BA 46 35 6B D6 76 F9 9F D4 58 30 E9 6B E4 81 D7 BD 6B 4E A5 35 6D 0F 31 B6 69 84 2C F0 35 67 AB 2F 9D 70 1C 73 7D 21 A4 A4 97 52 E7 22)
java.lang.
ArrayIndexOutOfBoundsException (Hexdump: A8 51 05 01 00 00 00 00 00 40 00 40 A9 FB B9 37 34 82 49 BA 46 35 6B D6 76 F9 9F D4 58 30 E9 6B E4 81 D7 BD 6B 4E A5 35 6D 0F 31 B6 69 84 2C F0 35 67 AB 2F 9D 70 1C 73 7D 21 A4 A4 97 52 E7 22)
2014-01-21 09:37:53,217 DEBUG IoFilterEvent - Event EXCEPTION_CAUGHT has been fired for session 3
[Ljava.lang.StackTraceElement;@1bbf1ca
org.apache.mina.filter.codec.ProtocolDecoderException: java.lang.Array
IndexOutOfBoundsException (Hexdump: A8 51 05 01 00 00 00 00 00 40 00 40 A9 FB B9 37 34 82 49 BA 46 35 6B D6 76 F9 9F D4 58 30 E9 6B E4 81 D7 BD 6B 4E A5 35 6D 0F 31 B6 69 84 2C F0 35 67 AB 2F 9D 70 1C 73 7D 21 A4 A4 97 52 E7 22)
at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:242)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:417)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1200(DefaultIoFilterChain.java:47)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:765)
at org.apache.mina.filter.logging.LoggingFilter.messageReceived(LoggingFilter.java:208)