为什么将字节数组读取到Object会抛出java.io.StreamCorruptedException?

时间:2021-07-05 04:50:24

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. But at my end, I have to convert the byte array to a POJO. While doing so, I am getting error java.io.StreamCorruptedException: invalid stream header:.

我需要从远程系统读取字节流。远程系统有自己的客户端API来读取字节。但在我的结尾,我必须将字节数组转换为POJO。这样做,我收到错误java.io.StreamCorruptedException:无效的流标题:

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转换为字节数组,然后将字节数组转换为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?

感谢有人可以帮助解决这里的错误吗?

1 个解决方案

#1


4  

Because you corrupted the stream. You shouldn't have had the serialized data in a String in the first place. The round trip back to byte[] is lossy. Just pass the byte[] array around.

因为你损坏了流。您不应该首先在String中使用序列化数据。往返byte []的往返是有损的。只需传递byte []数组即可。

Repeat after me. String is not a container for binary data. Write out 100 times ;-)

在我之后重复。 String不是二进制数据的容器。写出100次;-)

EDIT 0x54696765 is "Tige". You didn't have a serialized object in the first place. You already had the String.

编辑0x54696765是“Tige”。您首先没有序列化对象。你已经有了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,您也不需要关闭它。

#1


4  

Because you corrupted the stream. You shouldn't have had the serialized data in a String in the first place. The round trip back to byte[] is lossy. Just pass the byte[] array around.

因为你损坏了流。您不应该首先在String中使用序列化数据。往返byte []的往返是有损的。只需传递byte []数组即可。

Repeat after me. String is not a container for binary data. Write out 100 times ;-)

在我之后重复。 String不是二进制数据的容器。写出100次;-)

EDIT 0x54696765 is "Tige". You didn't have a serialized object in the first place. You already had the String.

编辑0x54696765是“Tige”。您首先没有序列化对象。你已经有了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,您也不需要关闭它。