程序是TCP的网络通信工具
使用 ByteArrayOutputStream和ObjectOutputStream实现序列化对象
在文件传输的时候会概率性反序列化出错
public class SerialUtils {
public static byte[] serialObject(Object obj) throws SerialException {
ByteArrayOutputStream bOut = null;
ObjectOutputStream out = null;
try {
try {
bOut = new ByteArrayOutputStream();
out = new ObjectOutputStream(bOut);
out.writeObject(obj);
return bOut.toByteArray();
} finally {
if (out != null) {
out.close();
}
if (bOut != null) {
bOut.close();
}
}
} catch (IOException e) {
e.printStackTrace();
throw new SerialException(e);
}
}
public static <T> T serialObject(byte[] data) throws SerialException {
ByteArrayInputStream bIn = null;
ObjectInputStream in = null;
try {
try {
bIn = new ByteArrayInputStream(data);
// 错误发生在这一行
in = new ObjectInputStream(bIn);
return (T) in.readObject();
} catch (ClassNotFoundException e) {
throw new SerialException("找不到实现反序列化的类", e);
} finally {
if (in != null) {
in.close();
}
if (bIn != null) {
bIn.close();
}
}
} catch (IOException e) {
e.printStackTrace();
throw new SerialException(e);
}
}
}
运行到 in = new ObjectInputStream(bIn); 这一行会概率性出错
java.io.StreamCorruptedException: invalid stream header: 37BB7395
冒号后面的数字完全随机
大概十次有四五次出错 具体概率完全看脸 跟抽卡似的
如果不是传文件就没有报过这类错误
数据校验完整 不存在数据损坏的问题 外面还有一层用于保证传输数据的完整性可靠
传文件我设置的单次发送大小为4M 我从4K该到8M都会有概率出现这个错误
大佬们帮忙看看咋处理
synchronized和去掉静态都没用