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 StreamCorruptedException
s 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 StreamCorruptedException
s and reported online.
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. |
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.