Why reading byte array to an Object throws java.io.StreamCorruptedException?
我需要从远程系统中读取字节流。远程系统有自己的客户端API来读取字节。但在我最后,我必须将字节数组转换为POJO。在这样做的同时,我得到了错误
为了测试功能,我编写了以下程序,将
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | public class ByteToObject { public static void main(String[] args) { try { final String str ="Tiger"; System.out.println(" Byte array for string '" + str +"' --> " + Arrays.toString(getByteArray(str))); System.out.println("Object read -->" + getObject(getByteArray(str))); } catch (Exception e) { e.printStackTrace(); } } private static byte[] getByteArray(final String str) throws Exception { return str.getBytes(CharEncoding.UTF_8); } private static Object getObject(final byte[] byteArray) throws Exception { InputStream byteArrayStream = null; ObjectInputStream inputStream = null; try { byteArrayStream = new ByteArrayInputStream(byteArray); inputStream = new ObjectInputStream(byteArrayStream); return inputStream.readObject(); } finally { if(null != byteArrayStream) { byteArrayStream.close(); } if(null != inputStream) { inputStream.close(); } } } } |
输出为:
1 2 3 4 5 6 7 8 | Byte array for string 'Tiger' --> [84, 105, 103, 101, 114] java.io.StreamCorruptedException: invalid stream header: 54696765 Object read --> null at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804) at java.io.ObjectInputStream.(ObjectInputStream.java:299) at com.demo.serialize.ByteToObject.getObject(ByteToObject.java:41) at com.demo.serialize.ByteToObject.main(ByteToObject.java:24) |
号
如果有人能帮上忙,你会感激吗?
因为你破坏了流。您不应该首先在
跟着我重复。
编辑0x54696765为"TIGE"。您最初没有序列化对象。你已经有了
注意,如果你要关闭包装的