use java8 functional
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-02-08 18:15:39 +08:00
parent 169bf6f290
commit d395b987e6
9 changed files with 69 additions and 39 deletions

View File

@@ -43,4 +43,9 @@ public class Constants {
public static String getEnv(String key) {
return SYS_ENV.get(key) == null ? "" : SYS_ENV.get(key);
}
/**
* 编码解码 byte 数组固定长度
*/
public static int DECODER_FRAMELENGTH = 100;
}

View File

@@ -46,7 +46,20 @@ public class NettyUtil {
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
return bos.toByteArray();
byte[] bytes = bos.toByteArray();
if (bytes.length > Constants.DECODER_FRAMELENGTH) {
logger.error("bytes length should not bigger than {}", Constants.DECODER_FRAMELENGTH);
return null;
} else if (bytes.length < Constants.DECODER_FRAMELENGTH) {
byte[] result = new byte[Constants.DECODER_FRAMELENGTH];
// 如果长度不足,填充
System.arraycopy(bytes, 0, result, 0, bytes.length);
return result;
}
return bytes;
}
public static Object toObject(byte[] bts) throws IOException, ClassNotFoundException {