[fix]加入原始数据解析长度判断,增强程序健壮性

This commit is contained in:
xinfengkun
2022-04-13 18:06:23 +08:00
parent 0038196596
commit eab20dc122

View File

@@ -35,17 +35,25 @@ public class RawUnpack {
private synchronized void unpack(ByteString bytes, RawData raw) {
// CupidLogUtils.w(TAG, "WS 原始数据=" + bytes.hex());
//读取magicCode
ByteString magicCode = bytes.substring(0, messageProtocol.getMagicCodeLength());
raw.setMagicCode(magicCode.toByteArray());
ByteString magicCode;
int magicCodeLength = messageProtocol.getMagicCodeLength();
if (bytes.size() < magicCodeLength) {
magicCode = bytes;
} else {
magicCode = bytes.substring(0, magicCodeLength);
raw.setMagicCode(magicCode.toByteArray());
}
// CupidLogUtils.w(TAG, "WS MagicCode=" + magicCode.hex());
if (Arrays.equals(magicCode.toByteArray(), Constants.RAW_MG)) {
int offsetLength = messageProtocol.getOffsetLength();
int outHeader = messageProtocol.getOutHeader();
//读取offset
ByteString offset = bytes.substring(messageProtocol.getMagicCodeLength(), messageProtocol.getMagicCodeLength() + messageProtocol.getOffsetLength());
ByteString offset = bytes.substring(magicCodeLength, magicCodeLength + offsetLength);
byte[] offsetB = offset.toByteArray();
raw.setOffset(offsetB, getOffsetValue(offsetB));
// CupidLogUtils.w(TAG, "WS 偏移量 bytes=" + offset.hex() + " 偏移量=" + raw.getOffsetValue());
//读取packageLength
ByteString packageLength = bytes.substring(messageProtocol.getMagicCodeLength() + messageProtocol.getOffsetLength(), messageProtocol.getOutHeader());
ByteString packageLength = bytes.substring(magicCodeLength + offsetLength, outHeader);
byte[] packageLengthB = packageLength.toByteArray();
raw.setPackageLength(packageLengthB, getPackageLengthValue(packageLengthB));
if (bytes.size() != raw.getPackageLengthValue()) {
@@ -55,7 +63,7 @@ public class RawUnpack {
}
// CupidLogUtils.w(TAG, "WS 数据包从长度 bytes=" + packageLength.hex() + " 数据包从长度=" + raw.getPackageLengthValue());
//读取header
ByteString header = bytes.substring(messageProtocol.getOutHeader(), raw.getOffsetValue());
ByteString header = bytes.substring(outHeader, raw.getOffsetValue());
raw.setHeader(header.toByteArray());
// CupidLogUtils.w(TAG, "WS Header=" + header.hex());
//读取payload