当我运行以下代码时,出现“数组越界”异常。基本上,我将两个 TLV 编码字符串从客户端发送到服务器,服务器解码 TLV 编码值并将它们打印到屏幕上。
打印解码值的代码
byte[] from_ca = new byte[65536];
int msg = is.read(from_ca, 0, 65536);
from_ca = Arrays.copyOfRange(from_ca, 0, msg);
Object[] decodeBytes = TLV.decode(from_ca);
String ca_id = (String) decodeBytes[0];
String cb_id = (String) decodeBytes[1];
TLV 类中用于解码的代码
public static Object[] decode(byte[] data) throws Exception {
int i = 0;
List<Object> tlvList = new ArrayList<Object>();
while (i < data.length) {
if (data[i] == TLV.ascii) {
int length = (256 * data[i + 2]) + data[i + 1];
tlvList.add(new String(Arrays.copyOfRange(data, i + 3, i + 3 + length), "US-ASCII"));
i = i + 3 + length;
} else if (data[i] == TLV.integer) {
int length = (256 * data[i + 2]) + data[i + 1];
tlvList.add(Integer.parseInt(new String(Arrays.copyOfRange(
data, i + 3, i + 3 + length), "US-ASCII")));
i = i + 3 + length;
} else if (data[i] == TLV.binary) {
int length = (256 * data[i + 2]) + data[i + 1];
tlvList.add(Arrays.copyOfRange(data, i + 3, i + 3 + length));
i = i + 3 + length;
}
} // end of loop
return tlvList.toArray(new Object[tlvList.size()]);
}
运行程序时出现以下异常:
an error has occurred
java.lang.ArrayIndexOutOfBoundsException: 1
at ServerConnection.run(Server.java: 131)
At java.lang.Thread.run(Unknown Source)
如果可能的话,我将不胜感激进一步调试问题甚至解决方案的帮助。