Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the defaultWriteObject function have to be called first when writing into an ObjectOutputStream?

When I read the about the Serializable interface in Thinking in java, there is a sentence that says:

If you use the default mechanism to write the non-transient parts of your object, you must call defaultWriteObject( ) as the first operation in writeObject( ), and defaultReadObject( ) as the first operation in readObject( ).

And in docs.oracle.com 5.6.2:

Adding writeObject/readObject methods - If the version reading the stream has these methods then readObject is expected, as usual, to read the required data written to the stream by the default serialization. It should call defaultReadObject first before reading any optional data. The writeObject method is expected as usual to call defaultWriteObject to write the required data and then may write optional data.

So If I don't call defaultWriteObject first, and if I write something else before that call, will there be any problem? I have tried it, but it seems that it still works well in my example. So If there is to be any problem, under what condition could it occur ?

like image 911
HonestManXin Avatar asked Apr 26 '13 14:04

HonestManXin


People also ask

How does ObjectOutputStream work in Java?

An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream.

What interface must an object implement before it can be written to a stream as an object?

Serializable interface can be written to streams. The class of each serializable object is encoded including the class name and signature of the class, the values of the object's fields and arrays, and the closure of any other objects referenced from the initial objects.

Is ObjectOutputStream serialized?

2.1 The ObjectOutputStream Class. Class ObjectOutputStream implements object serialization. It maintains the state of the stream including the set of objects already serialized. Its methods control the traversal of objects to be serialized to save the specified objects and the objects to which they refer.

How do you override a readObject and writeObject in Java?

Override default serialization to add validation In this usecase, you can use defaultReadObject() and defaultWriteObject() inside readObject() and writeObject() methods – to enable default serialization and deserialization.


1 Answers

Java Object Serialization Specification is vague on this subject:

Either ObjectOutputStream's defaultWriteObject or writeFields method must be called once (and only once) before writing any optional data that will be needed by the corresponding readObject method to restore the state of the object; even if no optional data is written, defaultWriteObject or writeFields must still be invoked once. If defaultWriteObject or writeFields is not invoked once prior to the writing of optional data (if any), then the behavior of instance deserialization is undefined in cases where the ObjectInputStream cannot resolve the class which defined the writeObject method in question.

Here's an old thread which gives an example case when problems might occur.

And here's a JBoss AS Jira ticket with another example.

like image 64
Andrew Logvinov Avatar answered Sep 28 '22 17:09

Andrew Logvinov