package safeluck.drive.evaluation.platformMessage.utils;
|
|
|
import com.safeluck.aykj.utils.ArrayUtils;
|
|
/**
|
* Created by zw on 2017/7/8.
|
* 蓝牙协议转义
|
*/
|
|
public class MessageEscaper {
|
|
public byte[] escape(byte[] source_bytes)
|
{
|
byte[] return_bytes = new byte[source_bytes.length*2];
|
|
int size = source_bytes.length;
|
int pos = 0;
|
for (int i = 0; i < size; i++)
|
{
|
if (i == 0||i==size-1)
|
{
|
return_bytes[pos++] = source_bytes[i];
|
continue;
|
}
|
if (source_bytes[i] == 0x7d)
|
{
|
return_bytes[pos++] = 0x7d;
|
return_bytes[pos++] = 0x01;
|
}
|
else if (source_bytes[i] == 0x7e)
|
{
|
return_bytes[pos++] = 0x7d;
|
return_bytes[pos++] = 0x02;
|
}
|
else
|
{
|
return_bytes[pos++] = source_bytes[i];
|
}
|
}
|
|
return ArrayUtils.subArray(return_bytes,0,pos);
|
|
|
}
|
|
public byte[] unescape(byte[] cmd_bytes)
|
{
|
//转义还原
|
int len = cmd_bytes.length;
|
byte[] return_bytes = new byte[cmd_bytes.length];
|
|
//int len = ByteHelper.HexString2Byte(cmd, cmd_bytes);
|
int pos = 0;
|
for (int i = 0; i < len; i++)
|
{
|
if (i == len - 1)
|
{
|
return_bytes[pos++] = cmd_bytes[i];
|
continue;
|
}
|
|
if (cmd_bytes[i] == 0x7d && cmd_bytes[i + 1] == 0x02)
|
{
|
return_bytes[pos++] = 0x7e;
|
i++;
|
continue;
|
}
|
else if (cmd_bytes[i] == 0x7d && cmd_bytes[i + 1] == 0x01)
|
{
|
return_bytes[pos++] = 0x7d;
|
i++;
|
continue;
|
}
|
else
|
{
|
return_bytes[pos++] = cmd_bytes[i];
|
}
|
}
|
return ArrayUtils.subArray(return_bytes,0,pos);
|
|
|
}
|
|
}
|