Monday, July 2, 2012

Serialization (Persistence) in Java

           Hi Friends, everyone who is acquainted with JAVA even distantly knows that Java platform allows us to create objects which can be reused in memory, but these objects can be reused as long as your java application or JVM is running i.e. alive. Once the execution of the program finishes or JVM ends its execution the objects also perish to exist. Think of a scenario where you need to save(persist) the current state of the instantiated object(with all its variables and attributes assigned some value) and reuse it at a later stage even beyond the lifetime of JVM. This is what all this article talks about. This can be done with the powerful  Serialization API provided by Java itself. We can flatten the objects using serialization and then re-expand them at a later stage.

           Using object serialization an object's state is saved into form of bytes on the physical memory like hard disk and it can be inflated into a live object  at a later stage from those bytes. The emphasis in this article will be on understanding the classes and methods provided by the Java serialization API. Persisting objects is one of the tougher challenges which can easily be met by making use of serialization . Major problem is solved by understanding the Serialization API clearly which is rather misunderstood and misinterpreted while using in different scenarios.

Lets start with the most basic and simple case.


Persisting An Object
               
            To persist or serialize a java object, the class of that object must implement the marker interface java.io.Serializable , which indicates the underlying API that this object can be serialized to the disk in form of bytes and reused at a later point of time.




SerializableClass
               As you have seen above , making a class as serializable is pretty simple. You just need to extend the Serializable interface. Doing this allows the JVM to identify this class as serializable.
All subtypes of a serializable class are themselves serializable.

                   Now we have made a class serializable but it needs to be persisted. It can be done by using      java.io.ObjectOuputStream class. 

ObjectOutputStream 
                    An ObjectOutputStream class writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. If the stream is a network socket stream, we could easily transfer a flattened object across a network wire and have it be rebuilt on the other side. The method writeObject() is used to write an object to the stream.

FlattenedObject



    


                                                      
            The real work of serializing or persisting the object is done in the highlighted line. The serialized file i.e. "serialize.object" will be saved in the project folder of your java app if the path is not mentioned for the file.




Inflating the serialized Object


InflatedObject


                 The real work of reconstructing the serialized object into live instance is done by the highlighted line. The serialized object i.e. "serialize.object" is taken up and type casted to the original object type. Method readObject() can read the raw bytes which were previously saved during the serialization of the object. The class file for the raw object being deserialized must be available to the JVM for the restoration of the object , because during serialization only object's state is saved  to the file and not the class file and its methods.

                   

No comments:

Post a Comment