Core Java

Parsing Value from StreamCorruptedException: invalid stream header Message

It is a relatively common occurrence to see StreamCorruptedExceptions thrown with a “reason” that states, “invalid stream header” and then provides the first part of that invalid stream header. Frequently, a helpful clue for identifying the cause of that exception is to understand what the invalid stream header is because that explains what is unexpected and causing the issue.

The StreamCorruptedException only has two constructors, one that accepts no arguments and one that accepts a single String describing the exception’s “reason”. This tells us that the “invalid stream header: XXXXXXXX” messages (where XXXXXXXX represents various invalid header details) are provided by the code that instantiates (and presumably throws) these StreamCorruptedExceptions rather than by that exception class itself. This means that it won’t always necessarily be the same formatted message encountered with one of these exceptions, but in most common cases, the format is the same with “invalid stream header: ” followed by the first portion of that invalid stream header.

This exception is commonly thrown by an ObjectInputStream. The Javadoc for that class has some useful details that help explain why the “StreamCorruptedException: invalid stream header” is encountered. The class-level Javadoc states, “Only objects that support the java.io.Serializable or java.io.Externalizable interface can be read from streams.” The Javadoc for the ObjectInputStream​(InputStream) constructor states (I added the emphasis), “Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified.”

As the quoted Javadoc explains, ObjectInputStream should be used with serialized data. Many of the cases of the “StreamCorruptedException: invalid stream header” message occur when a text file (such as HTML, XML, JSON, etc.) is passed to this constructor rather than a Java serialized file.

The following are examples of “ASCII” values derived from “invalid stream header” messages associated with StreamCorruptedExceptions and reported online.

Invalid Stream Header Value (HEX)Corresponding IntegersCorresponding
“ASCII” Value
Online References / Examples
00000000000 000 000 000https://stackoverflow.com/questions/44479323/exception-in-thread-main-java-io-streamcorruptedexception-invalid-stream-head
0A0A0A0A010 010 010 010https://issues.jenkins-ci.org/browse/JENKINS-35197
0A0A3C68010 010 060 104<hhttps://developer.ibm.com/answers/questions/201983/what-does-javaiostreamcorruptedexception-invalid-s/
20646520032 100 101 032dehttps://stackoverflow.com/questions/2622716/java-invalid-stream-header-problem
30313031048 049 048 0490101https://stackoverflow.com/questions/48946230/java-io-streamcorruptedexception-invalid-stream-header-30313031
32303138050 048 049 0562018https://stackoverflow.com/questions/49878481/jpa-invalid-stream-header-32303138
3C21444F060 033 068 079<!DOhttps://github.com/metasfresh/metasfresh/issues/1335
3c48544d060 072 084 077<HTMhttp://forum.spring.io/forum/spring-projects/integration/jms/70353-java-io-streamcorruptedexception-invalid-stream-header
3C6F626A060 111 098 106<obj
3C787364060 120 115 100<xsdhttps://stackoverflow.com/questions/29769191/java-io-streamcorruptedexception-invalid-stream-header-3c787364
41434544065 067 069 068ACEDhttps://stackoverflow.com/questions/36677022/java-io-streamcorruptedexception-invalid-stream-header-41434544
48656C6C072 101 108 108Hellhttps://stackoverflow.com/questions/28298366/java-io-streamcorruptedexception-invalid-stream-header-48656c6c
4920616D073 032 097 109I amhttps://stackoverflow.com/questions/34435188/java-io-streamcorruptedexception-invalid-stream-header-4920616d
54656D70084 101 109 112Temphttps://stackoverflow.com/a/50669243
54657374084 101 115 116Testjava.io.StreamCorruptedException: invalid stream header: 54657374
54686973084 104 105 115Thishttps://stackoverflow.com/questions/28354180/stanford-corenlp-streamcorruptedexception-invalid-stream-header-54686973
64617364100 097 115 100dasdhttps://stackoverflow.com/questions/50451100/java-io-streamcorruptedexception-invalid-stream-header-when-writing-to-the-stdo?noredirect=1&lq=1
70707070112 112 112 112pppphttps://stackoverflow.com/questions/32858472/java-io-streamcorruptedexception-invalid-stream-header-70707070
72657175114 101 113 117requhttps://stackoverflow.com/questions/8534124/java-io-streamcorruptedexception-invalid-stream-header-72657175
7371007E115 113 000 126sq ~https://stackoverflow.com/questions/2939073/java-io-streamcorruptedexception-invalid-stream-header-7371007e
77617161119 097 113 097waqahttps://coderanch.com/t/278717/java/StreamCorruptedException-invalid-stream-header
7B227061123 034 112 097{“pahttps://stackoverflow.com/questions/9986672/streamcorruptedexception-invalid-stream-header

The above examples show the “StreamCorruptedException: invalid stream header” message occurring for cases where input streams representing text were passed to the constructor that expects Java serialized format. The highlighted row is especially interesting. That entry (“ACED” in “ASCII” character representation) looks like what is expected in all files serialized by Java’s default serialization, but it’s not quite correct.

The “Terminal Symbols and Constants” section of the Java Object Serialization Specification tells us that java.io.ObjectStreamConstants defines a constant STREAM_MAGIC that is the “Magic number that is written to the stream header.” The specification further explains that ObjectStreamConstants.STREAM_MAGIC is defined as (short)0xaced and this can be verified in Java code if desired. The reason that particular entry led to an error is that it should be the hexadecimal representation that is “ACED” rather than the translated “ASCII” character representation. In other words, for that particular case, it was actually literal text “ACED” that was in the first bytes rather than bytes represented by the hexadecimal “ACED” representation.

There are many ways to translate the hexadecimal representation provided in the “StreamCorruptedException: invalid stream header” message to see if it translates to text that means something. If it is text, one knows that he or she is already off to a bad start as a binary serialized file should be used instead of text. The characters in that text can provide a further clue as to what type of text file was being accidentally provided. Here is one way to translate the provided hexadecimal representation to “ASCII” text using Java (available on GitHub):

private static String toAscii(final String hexInput)
{
   final int length = hexInput.length();
   final StringBuilder ascii = new StringBuilder();
   final StringBuilder integers = new StringBuilder();
   for (int i = 0; i < length; i+=2)
   {
      final String twoDigitHex = hexInput.substring(i, i+2);
      final int integer = Integer.parseInt(twoDigitHex, 16);
      ascii.append((char)integer);
      integers.append(String.format("%03d", integer)).append(" ");
   }
   return hexInput + " ==> " + integers.deleteCharAt(integers.length()-1).toString() + " ==> " + ascii.toString();
}

Streams of text inadvertently passed to ObjectInputStream‘s constructor are not the only cause of “StreamCorruptedException: invalid stream header”. In fact, any InputStream (text or binary) that doesn’t begin with the expected “stream magic” bytes (0xaced) will lead to this exception.

Published on Java Code Geeks with permission by Dustin Marx, partner at our JCG program. See the original article here: Parsing Value from StreamCorruptedException: invalid stream header Message

Opinions expressed by Java Code Geeks contributors are their own.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
José
José
1 year ago

Thank you for the fine explanation. It helped to understand the reasons for this error. But how and where to use this code snippet? An example with hibernate entity and repository could be fine.

Back to top button