简体   繁体   English

为什么将字节数组读取到对象会引发java.io.StreamCorruptedException?

[英]Why reading byte array to an Object throws java.io.StreamCorruptedException?

I have a requirement to read a stream of bytes from a remote system. 我需要从远程系统读取字节流。 The remote system has its own client API to read the bytes. 远程系统具有自己的客户端API来读取字节。 But at my end, I have to convert the byte array to a POJO. 但是到最后,我必须将字节数组转换为POJO。 While doing so, I am getting error java.io.StreamCorruptedException: invalid stream header: . 这样做时,出现错误java.io.StreamCorruptedException: invalid stream header:

To test the functionality, I wrote following program to convert a String to a byte array and then convert the byte array to an Object . 为了测试功能,我编写了以下程序,将String转换为byte array ,然后将字节数组转换为Object

public class ByteToObject {
  public static void main(String[] args) {
    try {
      final String str = "Tiger";
      System.out.println("\nByte array for string '" + str + "' --> \n" + 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();
        }
     } 
  }
}

The output is: 输出为:

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)

Appreciate if someone can help what is wrong here? 欣赏有人可以提供帮助吗?

Because you corrupted the stream. 因为您损坏了流。 You shouldn't have had the serialized data in a String in the first place. 首先,您不应该在String中拥有序列化的数据。 The round trip back to byte[] is lossy. 回到byte[]往返是有损耗的。 Just pass the byte[] array around. 只需传递byte[]数组即可。

Repeat after me. 跟着我重复。 String is not a container for binary data. String不是二进制数据的容器。 Write out 100 times ;-) 写出100次;-)

EDIT 0x54696765 is "Tige". 编辑 0x54696765为“大”。 You didn't have a serialized object in the first place. 首先,您没有序列化的对象。 You already had the String . 您已经有了String

NB You don't need to close the ByteArrayInputStream if you are closing the wrapping ObjectInputStream , and as that only wraps a ByteArrayInputStream you don't really need to close that either. 注意:如果要关闭包装的ObjectInputStream ,则不需要关闭ByteArrayInputStream ,因为它只包装了ByteArrayInputStream您实际上也不需要关闭它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM