fix(iot): fix JT808 escape logic to exclude packet delimiters
This commit is contained in:
@@ -38,26 +38,23 @@ public class Jt808ProtocolUtil {
|
||||
}
|
||||
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||
// 写入起始部分
|
||||
for (int i = 0; i < start; i++) {
|
||||
baos.write(bs[i]);
|
||||
}
|
||||
// 处理需要反转义的部分
|
||||
for (int i = start; i < end - 1; i++) {
|
||||
if (bs[i] == 0x7d && bs[i + 1] == 0x01) {
|
||||
baos.write(0x7d);
|
||||
i++;
|
||||
} else if (bs[i] == 0x7d && bs[i + 1] == 0x02) {
|
||||
baos.write(0x7e);
|
||||
i++;
|
||||
// 只处理 start 到 end 范围内的数据(不包含 start 之前和 end 之后的数据)
|
||||
// 这样返回的数组就是纯净的、去除了首尾标识符且已反转义的数据包
|
||||
for (int i = start; i < end; i++) {
|
||||
if (bs[i] == 0x7d && i + 1 < end) { // 确保 i+1 不越界
|
||||
if (bs[i + 1] == 0x01) {
|
||||
baos.write(0x7d);
|
||||
i++;
|
||||
} else if (bs[i + 1] == 0x02) {
|
||||
baos.write(0x7e);
|
||||
i++;
|
||||
} else {
|
||||
baos.write(bs[i]);
|
||||
}
|
||||
} else {
|
||||
baos.write(bs[i]);
|
||||
}
|
||||
}
|
||||
// 写入结束部分
|
||||
for (int i = end - 1; i < bs.length; i++) {
|
||||
baos.write(bs[i]);
|
||||
}
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user