Sunday, February 15, 2009

Serialization

Java Serialization(serializable):
The process of saveing an object data to a file is called serialization.
writeObject(o)
The process of retrieving an object data from the file is called deserialization.
By using read object method of ObjectInputStream we can achieve deserialization.
ObjectInputStream ----->FileInputStream------>abc.txt
serialization Example:
class Dog implements Serializable
{
int i=10;
int j=20;
transient int k=30;
}
public class Myser1 {
public static void main(String arg[])throws Exception

{
Dog d1=new Dog();
FileOutputStream fos=new FileOutputStream("abc.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(d1);

FileInputStream fis=new FileInputStream("laxman.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Dog d2=(Dog)ois.readObject();
System.out.println(d2.i+"......"+d2.j);
}
}

It is suggestable our java bean classes should implements serialization.
Ois.readObject() method returns an object in the java.lang.Object form .we should perform the type casting.

  • All the Objects doesn’t have the capability of saving to the file .Only serializable Objects having this capability.
  • An object is said to be serializable if and only if the corresponding class implements serializable interface(directly or indirectly).
  • The serializable interface doesn’t contain any method and it is an example of marker interface.
  • If an object is non-serializable then we are not allow to save this object to the file.vilation leads to RTE saying java.io.net.serializableException.
  • All the wrapper classes,collection classes and arrays of primitives already implemented serializable interface.Hence these are serializable objects.
  • If you don’t want to save ,the value of a particular instance variable while performing serialization ,then we have to declare those variable as transient.
  • If we are declearing a variable as the transient ,while saving an object to the file JVM ignores ,the value of this variable ,instead of saving original value ,JVM stores default value for the transient variables.Hence transient means ‘not to serializable”.
  • Static variables are not part of object state hence they never participated in the serialization process.A single copy of the static variable will exist and all the objects will share that copy.
  • Final variable also never participated in the serialization.there is no effect of declearing a final or a static variable as transient.
Object Graphs:
When ever we are saving an objects to the file all the objects which are reachable from that object by default saving to the file.This group of object is called ObjectGraph.
Example:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Dog1 implements Serializable
{
Cat c=new Cat();
}
class Cat implements Serializable
{
int i=10;
Rat r=new Rat();
}
class Rat implements Serializable
{
int j=20;
}

class SerDemo2
{
public static void main(String arg[])throws Exception
{
Dog1 d=new Dog1();
FileOutputStream fos=new FileOutputStream("abc1.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(d);
FileInputStream fis=new FileInputStream("abc1.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Dog1 d1=(Dog1)ois.readObject();
System.out.println(d1.c.i);
System.out.println(d1. c.r.j);
}
}

When ever we are saving an object to the file.All the object present in its objects graph by default will save to the file .Hence all the objects present in the object graph also must be serializable .voilation leads to RTE saying NotSerializable Exception”.
Example:
class Dog implements serializable
{
Cat c=new Cat();
}
Class Cat
{
Int j=20;
}
Class SerialDemo
{
public static void main(String arg[])
{
Dog d=new Dog();
FileOutputStream fos=new FileOutputStream(“abc.txt”);
ObjectOutputStream oos=new ObjectOutputStream(fos);
Oos.writeObject(d);
FileInputStream fis=new FileInputStream(“abc.txt’);
ObjectInputStream ois=new ObjectInputStream(fis);
Dog d1=(Dog)ois.readObject();
System.out.println(d1.c.j);
System.out.println(d1. c.j);
}
}
Customized Serialization:
During default serialization there may be a chance of loss of information .To over come these problems ,we can perform customized serialization .
We can perform customized serialization by using the following two call back methods.
1.private void writeObject(OutputStream os)
JVM calls this writeObject method at the time of serialization.
2.private void readObject(inputStream is)
the JVM calls this method at the time of deserialization automatically.
The above two methods are called by JVM automatically at the time of serialization and deserialization .Hence these methods are considered as call back methods.
Customized Serialization Example:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;



class Dog2 implements Serializable
{
transient Cat1 c=new Cat1();
private void writeObject(ObjectOutputStream os)throws Exception
{
os.defaultWriteObject();
int i=c.j;
os.writeInt(i);
}
private void readObject(ObjectInputStream is)throws Exception
{
is.defaultReadObject();
int i=is.readInt();
c=new Cat1();
c.j=i;
}
}
class Cat1
{
int j=20;
}
public class SerDemo3
{
public static void main(String arg[])throws Exception
{
Dog2 d=new Dog2();
FileOutputStream fos=new FileOutputStream("abc.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(d);
FileInputStream fis=new FileInputStream("abc.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Dog2 d1=(Dog2)ois.readObject();
System.out.println(d1.c.j);
}
}

0 comments:

Post a Comment