Sunday, July 22, 2012

Customized Serialization

<<Previous                                                                    Next>>

package com.javamonkeys.serialization;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class User implements Serializable{
 transient UserDetails ud = new UserDetails();
}
class UserDetails implements Serializable{
 int udId=10;
}

public class ObjectGraphExample {
 public static void main(String[] args)throws IOException,ClassNotFoundException {
  User user = new User();
  FileOutputStream fiOutputStream = new FileOutputStream("D:\\serializablefile.ser");
  ObjectOutputStream oStream = new ObjectOutputStream(fiOutputStream);
  oStream.writeObject(user);
  
  FileInputStream fileInputStream = new FileInputStream("D:\\serializablefile.ser");
  ObjectInputStream oInputStream = new ObjectInputStream(fileInputStream);
  User usr = (User)oInputStream.readObject();
  System.out.println(usr.ud.udId);
 }

}


  • During Serialization there may be chance of loss of information because of transient variable.
  • In the above program before serialization we can access udId value but, after de-serialization we can’t access udId value. Because in the serialization process it will save null.  We will get NullPointerException in the line 27.
  • We can recover this lost information by using Customized Serialization.
  • We can achieve Customized Serialization by using following two methods.
private void writeObject(OutputStream os)
  • This method will be executed automatically at the time of Serialization.  Hence if we need to perform any extra activity at the time of serialization, we have to define the logic inside this method.
private void readObject(InputStream is)
  •  This method will be executed at the time of time of de-serialization automatically.  At the time of de-serialization if we need to perform any extra activity, we have to define the logic inside this method.
  • We have to keep these two methods in the corresponding class of Serialized object that is, at the time of User object serialization if we want to perform any extra activity we have to define these two methods in the User class.


package com.javamonkeys.serialization;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class UserDetails{
 int udId=10;
}
class User implements Serializable{
 transient UserDetails ud = new UserDetails();
 private void writeObject(ObjectOutputStream os)throws IOException{
  os.defaultWriteObject();
  int k = ud.udId+100;//encrypting using some algorithm
  os.writeInt(k);
 }
 private void readObject(ObjectInputStream is)throws IOException, ClassNotFoundException{
  is.defaultReadObject();
   ud = new UserDetails();
  int k=is.readInt();
  //System.out.println(k);
  ud.udId=k-100;//Decrypting using the same algorithm used in above method.
 }
}


public class CustomizedSerializationExample {
 public static void main(String[] args)throws IOException, ClassNotFoundException {
  User user = new User();
  System.out.println(user.ud.udId);
  FileOutputStream fiOutputStream = new FileOutputStream("D:\\serializablefile.ser");
  ObjectOutputStream oStream = new ObjectOutputStream(fiOutputStream);
  oStream.writeObject(user);
  
  FileInputStream fileInputStream = new FileInputStream("D:\\serializablefile.ser");
  ObjectInputStream oInputStream = new ObjectInputStream(fileInputStream);
  User usr = (User)oInputStream.readObject();
  System.out.println(usr.ud.udId);
 }

}




No comments:

Post a Comment