java.io.StreamCorruptedException: invalid stream header: 48656C6C

11,945

Solution 1

ppBytes must hold the bytes of an serialized object. See below a short example.

byte[] buffer;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos)) {
    oos.writeObject("Hello World");
    buffer = bos.toByteArray();
    for (int i : buffer) {
        System.out.printf("%02X ", i & 0xFF);
    }
    System.out.println("");
}

try (ByteArrayInputStream bis = new ByteArrayInputStream(buffer);
        ObjectInputStream ois = new ObjectInputStream(bis)) {
    String input = (String) ois.readObject();
    System.out.println("input: " + input);
}

output

//                   H  e  l  l  o     W  o  r  l  d
AC ED 00 05 74 00 0B 48 65 6C 6C 6F 20 57 6F 72 6C 64 
input: Hello World

In the below example the buffer contains the byte representation of the String Hello World. To read those bytes with an ObjectInputStream will fail with java.io.StreamCorruptedException: invalid stream header: 48656C6C. As an serialized String object is expected.

byte[] buffer;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
    bos.write("Hello World".getBytes(StandardCharsets.ISO_8859_1));
    buffer = bos.toByteArray();
    for (int i : buffer) {
        System.out.printf("%02X ", i & 0xFF);
    }
    System.out.println("");
}

try (ByteArrayInputStream bis = new ByteArrayInputStream(buffer);
        ObjectInputStream ois = new ObjectInputStream(bis)) {
    String input = (String) ois.readObject();
    System.out.println("input: " + input);
}

output

// H  e  l  l  o     W  o  r  l  d
   48 65 6C 6C 6F 20 57 6F 72 6C 64 
   Exception in thread "main" java.io.StreamCorruptedException: invalid \
      stream header: 48656C6C

Solution 2

You can do this by converting a String to a byte[] and back again, to String:

String hello = "Hello world";
byte[] bytes = hello.getBytes( "iso-8859-1" );  // or utf-8
// send
String world = new String( bytes, "iso-8859-1" ); // or utf-8
System.out.println( hello );
System.out.println( world );

It is more reliable to read and write (String) objects, which bypasses the encoding/decoding gamble:

String hello = "Hello world";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeUTF( hello );
oos.flush();
byte[] bytes = baos.toByteArray();
// send
ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
ObjectInputStream ois = new ObjectInputStream( bais );
String world = ois.readUTF();
Share:
11,945
Kanmani
Author by

Kanmani

Updated on October 14, 2022

Comments

  • Kanmani
    Kanmani over 1 year

    I am using netty client server for communication . The message is received successfully as byte array . When I convert byte array to ObjectInputStream I get the exception

    java.io.StreamCorruptedException: invalid stream header: 48656C6C
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
    at nettyClientServer2.PongHandler.messageReceived(PongHandler.java:99)
    at org.jboss.netty.channel.SimpleChannelHandler.handleUpstream(SimpleChannelHandler.java:88)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
    at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
    at org.jboss.netty.handler.execution.ChannelUpstreamEventRunnable.doRun(ChannelUpstreamEventRunnable.java:43)
    at org.jboss.netty.handler.execution.ChannelEventRunnable.run(ChannelEventRunnable.java:67)
    at org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor$ChildExecutor.run(OrderedMemoryAwareThreadPoolExecutor.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
    

    This is how I convert..

    byte[] ppBytes=pptmp.status;
    ObjectInputStream input = null;
    input = new ObjectInputStream(new ByteArrayInputStream(ppBytes));