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 ?
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.
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.
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.
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.
Java Object Serialization Specification is vague on this subject:
Either
ObjectOutputStream
'sdefaultWriteObject
orwriteFields
method must be called once (and only once) before writing any optional data that will be needed by the correspondingreadObject
method to restore the state of the object; even if no optional data is written,defaultWriteObject
orwriteFields
must still be invoked once. IfdefaultWriteObject
orwriteFields
is not invoked once prior to the writing of optional data (if any), then the behavior of instance deserialization is undefined in cases where theObjectInputStream
cannot resolve the class which defined thewriteObject
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With