3

I have some very basic serialization code:

void serializeObjectToFile(Serializable serializable, File file) {
    ObjectOutputStream stream = null;
    try {
        stream = new ObjectOutputStream(new FileOutputStream(file));
        stream.writeObject(serializable);
        stream.flush();
    } catch (Exception exception) {
        logger.error("Failed to serialize object: {}.", serializable, exception);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException exc) {
                logger.error("Error closing stream.", exc);
            }
        }
    }
}

And also some very deserialization code:

Object deserializeObjectFromFile(File file) {
    ObjectInputStream oiStream = null;
    try {
        oiStream = new ObjectInputStream(new FileInputStream(file));
        return oiStream.readObject();
    } catch (Exception exc) {
        logger.error("Exception loading object from file '{}'.", file.getAbsolutePath(), exc);
    } finally {
        if (oiStream != null) {
            try {
                oiStream.close();
            } catch (IOException exc) {
                logger.error("Error closing stream.", exc);
            }
        }
    }
    return null;
}

This code works fine for most objects. But for some objects I get the following exception:

java.io.StreamCorruptedException: unexpected end of block data
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350) ~[na:1.6.0_29]
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) ~[na:1.6.0_29]
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350) ~[na:1.6.0_29]
at java.util.ArrayList.readObject(ArrayList.java:593) ~[na:1.6.0_29]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_29]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_29]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_29]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_29]
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1848) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) ~[na:1.6.0_29]
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) ~[na:1.6.0_29]
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350) ~[na:1.6.0_29]
at my.CustomClass.deserializeObjectFromFile(CustomClass.java:79) ~[classes/:na]

I do not try to serialize more than one object per file (as suggested by other questions on the topic). The code works for most objects, but not for some. Since ArrayList.readObject() appears in the stack trace: has that got something to do with the problem? If so, what? To my surprise and dismay I found nothing on the net concerning that problem...

I appreciate any pointers on where to go from here...!

1
  • 1
    What's it an ArrayList of? ArrayList itself is practically certain not to be the culprit here, but a class of your own with a custom readObject()/writeObjext() pair definitely is. Commented Oct 4, 2014 at 0:23

1 Answer 1

1

I found the problem: One of the objects deep down in the object graph implemented the following method:

private void writeObject(java.io.ObjectOutputStream out) throws IOException

without actually writing its internals to the ObjectOutputStream and without throwing an exception (the method was implemented for validation purposes). So the solution: when implementing this method, either throw an Exception of some sort or write the internals of the object to the ObjectOutputStream. Easiest is to call

out.defaultWriteObject();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.