我想检查中的字节是ostream
表示序列化对象还是字节数组:
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(ostream);
out.writeObject(new TestClass());
out.flush();
out.close();
byte[] bytes = ostream.toByteArray();
isSerializedObject(new ObjectInputStream(
new ByteArrayInputStream(bytes)))); // returns false
isSerializedObject(new ByteArrayInputStream(bytes))); // returns true
的代码isSerializedObject
如下所示:
public static boolean isSerializedObject(InputStream istream) throws Exception {
int size = 2;
PushbackInputStream pis = new PushbackInputStream(istream, size);
byte[] buffer = new byte[size];
pis.read(buffer);
// serialized data can be identified by the following two bytes
boolean flag = buffer[0] == 0xAC && buffer[1] == 0xED;
pis.unread(buffer);
return flag;
}
有人可以解释为什么我使用 an时isSerializedObject
返回但使用 a 时返回吗?false
ObjectInputStream
true
ByteArrayInputStream