Serialization can be achieved by a marker interface in Java "serializable"
you never need to define any method even the readObject and writeObject
but if you want some tweaks or want to change default protocol then
override these two method.
private void readObject(ObjectInputStream in) throws IO
private void writeObject(ObjectOutputStream os) throws IO,ClassNotFound
How does it work
Serialization-
1-writes the static data which consist of two parts
streamData+streamVersion = staticData.
streamData - data of actual object needs to be serialized
streamVersion-JVM version needs in desrialization
2-after it writes the class metadata information
what does metadata contains-it basically contains the, length of class name of class,serialVersionUID, various flags, and the fields details of the class
3-Then in the same fashion it will recursively iterate in the whole hierarchy and write the metaData information of all the parent class till reaches java.lang.Object class.
4-Now it writes the actual data associated with the object, so this time it starts from the superclass
Now what else have left so we have Externalization
1-serialization is recursive algorithm
2-lots of data and metaData till object is written and at the time of deserialization it also verifies it using reflection
3-if you have not provided the serailVersionUID , then JVM calculates its based on the structure of the class
4-No constructor is called so if wants some tweaks override the method readObject and writeObject
These all 4 are performance issue, so Get rid of these Externalization comes AAh!
1-In externalization you MUST need to have default no arg constructor which is calling super() no arg or super argument constructor, it will be called at the time of deserialization
Note- This constructor must be public
2-it is not marker interface and have two methods readexternal and writeExternal
total control of serializing is under these two methods
needs also to take care of the inherited variable from the parent class
3-Externalization interface extends the serialization interface
Some Tweaks
1-if a class implements Externalization and its parent not
in this case you need to persist the superclass field and super class should have constructor which would be called by base class constructor
2-If base class also implements externalizable
then base class have its own method of readExternal and writeExternal
call super.writeExternal() and super.readExternal()
you never need to define any method even the readObject and writeObject
but if you want some tweaks or want to change default protocol then
override these two method.
private void readObject(ObjectInputStream in) throws IO
private void writeObject(ObjectOutputStream os) throws IO,ClassNotFound
How does it work
Serialization-
1-writes the static data which consist of two parts
streamData+streamVersion = staticData.
streamData - data of actual object needs to be serialized
streamVersion-JVM version needs in desrialization
2-after it writes the class metadata information
what does metadata contains-it basically contains the, length of class name of class,serialVersionUID, various flags, and the fields details of the class
3-Then in the same fashion it will recursively iterate in the whole hierarchy and write the metaData information of all the parent class till reaches java.lang.Object class.
4-Now it writes the actual data associated with the object, so this time it starts from the superclass
Now what else have left so we have Externalization
1-serialization is recursive algorithm
2-lots of data and metaData till object is written and at the time of deserialization it also verifies it using reflection
3-if you have not provided the serailVersionUID , then JVM calculates its based on the structure of the class
4-No constructor is called so if wants some tweaks override the method readObject and writeObject
These all 4 are performance issue, so Get rid of these Externalization comes AAh!
1-In externalization you MUST need to have default no arg constructor which is calling super() no arg or super argument constructor, it will be called at the time of deserialization
Note- This constructor must be public
2-it is not marker interface and have two methods readexternal and writeExternal
total control of serializing is under these two methods
needs also to take care of the inherited variable from the parent class
3-Externalization interface extends the serialization interface
Some Tweaks
1-if a class implements Externalization and its parent not
in this case you need to persist the superclass field and super class should have constructor which would be called by base class constructor
2-If base class also implements externalizable
then base class have its own method of readExternal and writeExternal
call super.writeExternal() and super.readExternal()
Comments
Post a Comment