Header Ads

Introduction to Object Serialization in Java part-1


Object Serialization in Java:-
Let us assume that we  have drawn a picture on applet paint(2D graphics programming) and the picture became so sexy and cool,we want to save the paint to use further.

It is a common pain for everybody that if we close the Applet the picture will be gone.So what will do to protect our picture??

Again lets say we are playing a game and after sometimes I want to quite that for some other work. Now when I start the game again, It will start from beginning. I want to save it in its current position. 



let's take another case, where we have three objects in our program during run time.It means three objects are stored in memory.This memory management is done by JVM. Now out of these three objects, I might need the second one with the current state in my program in some other time or next time.How to use it?? 

If we close the program the object will be garbage collected.Memory will be freed. What could be the solution to protect my required object? How can I make use of the power of java to use reusable objects??
                              One more problem could be if programmer want to use an object in remote, and if both they are in same virtual space,it is easy to use that object by using call by reference. If they are in different virtual space??You have only one option left that is call by value.
Now let's come to the point..how to save the applet paint??
There are two ways:-
1)Do all the steps, you have done till now i.e redo,all the steps to get it back-No-this not good solution.99%chances are there, to forget what step we have done previously.
2)Simple,hibernate your machine so that JVM continue to run.Yes,it is good solution not always the best.
What is the catch from the above discussion??
 Yes,you get it right!!The dots are:-
                                 (i)create an object.
                                 (ii)Allocate the memory but do not free the space while closing the main process.
                                 (iii)Make sure your JVM runs to see the pic.
So,any point of time if we can connect the three dots we can get the required paint/object isn't it??
What Serialization offers??
                         Serialization offers to connect the dots.It saves the object and its current state.How ?? let see..Serialization creates a stream of byte for an object and its state.Finally when you create a file, object and save the serialization object it actually store the stream of bytes to file,memory buffer,or can be transmitted over network.

Serialization also offers a strong API that will help you to create the sequence of byte stream.
How it can be implemented??
Cool!!Now we need to serialize the object.
              To serialize an object actually depicts:
              (i)Converts an object and its state to a byte stream.
              (ii)This byte stream can be saved to a memory location or a storage-i.e file.
              (iii)The extension of the file may be ".ser".
              (iv)The .ser file is machine readable,not human readable.
              (v)The stored file should not be  altered or if so JVM should not process it.
              (vi)The .ser file should be readable by JVM when configured.
For the first rule we must implement java.io.serialization interface.
So,the defination or signature of the class will be public class myClass implements serializable.
Well java has come up with something that will connect these dots.That is object serialization.By this technique one can save the application and load the state,where it was getting saved. 

Somebody might ask why not a normal save.why this?

Well,the answer is very easy,save operation needs more attention-say your application and object state requires more byte of data than you thought of while starting your program.So every time you modify your application,it requires to update specification of the storage medium,secondly you need to configure save and again restore mechanism.
    
Advantages:-
1)Reduced time to write code for save and retrive the object and its state.
2)As it is giving a new file format it reduces the complexity of such coding.
3)Reusable objects,when converted to stream of byte,can move any location without a problem.
4)While working with remote object,when we want to re-reference to the remote object,if both are in same virtual space,they can talk by reference but when they are on different virtual space..well,this time you need to invoke pass by value and serialization.
5)Easy API for serialization.


Diadvantages:-
1)Java Serialization is an expensive operation in terms of large output byte file.
2)It might effect the performance.
3)Easy to get corrupted.
4)caching objects in the stream needs to be implemented very carefully.
                          i.e  OutputStream out=new objectOutputStream();
                                 myObject obj=new myObject();
                                 obj.setstrate(100);
                                 out.writeObject(100);
                                 obj.SetStrate(200);
                                 out.WriteObject(200);This is not saved.
In order to set another value,we need to close outputStream and open again.
5)While overwrite an object/class,this may throw a java.io.InvalidClassExcaption.
6)Serialization works for only public constructors,so there may be a security threat that anybody can access that class.
7) it is bit slow and the backend technique is kind of known to user.


In Serialization,we convert an "in memory" object to  byte stream.This bytestream when saved to a file is not readable to user.This process  is also called deflating or marshalling an object.
There are three different ways so that we can implement serialization testing:-
        (I)default protocol
        (II)customized default protocol
        (III)own protocol
I wanted to save project1.myTime@4741d6Flattened time: Wed Feb 01 16:56:45 IST 2012
This is what i got in the file
Ԁ牳ༀ牰橯捥ㅴ洮呹浩깥좟ʜĀL琄浩瑥က橌癡⽡瑵汩䐯瑡㭥灸牳฀慪慶甮楴⹬慄整橨Ɓ奋ᥴ 砀睰 Ā㠵뎪碈
       



Powered by Blogger.