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;
}
}
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.core.filterchain.IoFilterAdapter.messageReceived(IoFilterAdapter.java:109)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:417)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.fireMessageReceived(DefaultIoFilterChain.java:410)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.read(AbstractPollingIoProcessor.java:710)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.process(AbstractPollingIoProcessor.java:664)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.process(AbstractPollingIoProcessor.java:653)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.access$600(AbstractPollingIoProcessor.java:67)
at org.apache.mina.core.polling.AbstractPollingIoProcessor$Processor.run(AbstractPollingIoProcessor.java:1124)
at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:64)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ArrayIndexOutOfBoundsException
2014-01-21 09:37:53,217 DEBUG ProtocolCodecFilter - Processing a MESSAGE_RECEIVED for session 3
--------------------解码-----------------
**********************************解码长度:15
-89-7775-125-95-10012061-2730-65117-15026
***********************************packeglen1:58654
2014-01-21 09:37:53,218 DEBUG IoFilterEvent - Firing a MESSAGE_RECEIVED event for session 3
2014-01-21 09:37:53,218 DEBUG IoFilterEvent - Event MESSAGE_RECEIVED has been fired for session 3
2014-01-21 09:37:53,484 INFO LoggingFilter - RECEIVED: HeapBuffer[pos=0 lim=79 cap=128: A8 51 05 01 00 00 00 00 00 40 00 40 A9 FB B9 37...]
2014-01-21 09:37:53,485 DEBUG ProtocolCodecFilter - Processing a MESSAGE_RECEIVED for session 3
从错误信息来看 应该是 (byteb[13 + packeglen1 +1] & 0xFF) == 0x1A 的数组越界(说明一下不是每次加解密都会越界),请问这是什么原因,最关键是如何解决呢?谢谢!
顺便贴出编码代码:
package Coder;
import java.nio.charset.Charset;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;
public class CmccSipcCodecFactory implements ProtocolCodecFactory {
private final CmccSipcEncoder encoder;
private final CmccSipcDecoder decoder;
public CmccSipcCodecFactory()
{
this(Charset.defaultCharset());
}
public CmccSipcCodecFactory(Charset charSet)
{
this.encoder = new CmccSipcEncoder(charSet);
this.decoder = new CmccSipcDecoder(charSet);
}
@Override
public ProtocolDecoder getDecoder(IoSession session) throws
Exception {
return decoder;
}
@Override
public ProtocolEncoder getEncoder(IoSession session) throws
Exception {
return encoder;
}
}