Netty 自定义编解码器
// byte 转int
public static int [] byteToInt(byte [] arr) throws Exception {
int len = arr.length;
if (len > 0){
int [] temp = new int[len];
for (int i = 0; i < len; i++){
temp[i] = Byte.toUnsignedInt(arr[i]);
}
return temp;
}
throw new Exception("自定义类型转换异常: byte to int exception");
}
//int 转 byte
public static byte[] intToByte(int [] data){
int len = data.length;
byte [] temp = new byte[len];
for (int i = 0 ; i <len; i++){
if (data[i] > 127){
temp[i] = (byte)(data[i]&0x7F + 128);
}else {
temp[i] = (byte) data[i];
}
}
return temp;
}
// byte 转int
public static int [] byteToInt(byte [] arr) throws Exception {
int len = arr.length;
if (len > 0){
int [] temp = new int[len];
for (int i = 0; i < len; i++){
temp[i] = Byte.toUnsignedInt(arr[i]);
}
return temp;
}
throw new Exception("自定义类型转换异常: byte to int exception");
}
//int 转 byte
public static byte[] intToByte(int [] data){
int len = data.length;
byte [] temp = new byte[len];
for (int i = 0 ; i <len; i++){
if (data[i] > 127){
temp[i] = (byte)(data[i]&0x7F + 128);
}else {
temp[i] = (byte) data[i];
}
}
return temp;
}