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

Kanmani picture Kanmani · Feb 3, 2015 · Viewed 11.3k times · Source

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));

Answer

SubOptimal picture SubOptimal · Feb 3, 2015

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