Saturday, April 21, 2012

JAVA.IO.SERIALIZATION: Custom Serialization Process (writeObject,readObject,writeReplace,readResolve)

JAVA.IO.SERIALIZATION:
>>>> USE OF "private void writeObject(ObjectOutputStream objOut)throws IOException{}",

>>>> USE OF "private void readObject(ObjectInputStream objIn)throws IOException{}",

>>>> USE OF "ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException{}";

>>>> USE OF "ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException{}";

Note: when we use writeReplace(..) method to replace the serializing object and serialize some other object then we must implement the readResolve(..) method in other object.
See example below, writeReplace() in Employee class, and readResolve() in Organization class.
################################################################################################

package io_serialization;

import java.io.Serializable;

public class Employee implements Serializable{

private static final long serialVersionUID = 1L;
int empId;
public Employee() {
super();
System.out.println("Employee instantialted..");
}

}

***********************************************************************
package io_serialization;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;

public class Engineer extends Employee{
private static final long serialVersionUID = 1L;
int age;
String name;
String address;
transient int salary=0;
//Organization org=null;

public Engineer(int age, String name, String address,String orgName,int orgId) {
super();
this.age = age;
this.name = name;
this.address = address;
this.salary=22000;
empId=101;
//org=new Organization(orgId,orgName);
System.out.println("Engineer object instantiated_param-constructor");
}

public Engineer(Organization o) {
System.out.println("Engineer object instantiated_param-constructor_org");
System.out.println("OrgData: "+o.orgId+" | "+o.orgName);
}

public String toString(){
return "Emp: "+name+" | "+age+" | "+address+" |transientVar "+salary+" |empId "+empId;
}

Engineer() {
super();
System.out.println("Engineer object instantiated_def-constructor");
}

/*private void writeObject(ObjectOutputStream objOut)throws IOException{
objOut.defaultWriteObject();
objOut.writeInt(salary);
}

private void readObject (ObjectInputStream objIn)throws IOException, ClassNotFoundException{
objIn.defaultReadObject();
salary=objIn.readInt();
}*/

public Object writeReplace()throws ObjectStreamException{
Organization org=new Organization(102,"abcd");
return org;
}

/*public Object readResolve()throws ObjectStreamException{
return new Employee();
}*/
}
**************************************************************************************

package io_serialization;

import java.io.Serializable;

public class Organization implements Serializable {
private static final long serialVersionUID = 1L;
int orgId;
String orgName="India pvt.ltd.";
public Organization() {
super();
System.out.println("Org instantiated.._default");
}

public Organization(int orgId, String orgName) {
super();
this.orgId = orgId;
this.orgName = orgName;
System.out.println("Org instantiated.._param");

}

public String toString(){
return "OrgData: "+orgId+" | "+orgName;
}

public Object readResolve(){
return new Engineer(this);
}
}
**************************************************************************************
package io_serialization;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SerializeObject {
public static void main(String[] args) {
ObjectOutputStream objOut=null;
try{
Engineer e=new Engineer(25,"abhinav","noida","BCDS",102);
System.out.println("Emp obj before serialization: "+e);
objOut=new ObjectOutputStream(new FileOutputStream("emp.txt"));
objOut.writeObject(e);
objOut.flush();
System.out.println("Emp object serialized..");
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(objOut!=null){
objOut.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
**************************************************************************************
package io_serialization;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class DeserializeObject {
public static void main(String[] args) {
ObjectInputStream objIn=null;
try{
Engineer e=null;
objIn=new ObjectInputStream(new FileInputStream("emp.txt"));
e=(Engineer)objIn.readObject();
System.out.println("Deserialized emp obj: "+e);
}catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
if(objIn!=null){
try {
objIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
*********************************************************************************
OUTPUT SERIALIZATION>>>


Employee instantiated..
Engineer object instantiated_param-constructor
Emp obj before serialization: Emp: abhinav | 25 | noida |transientVar 22000 |empId 101
Org instantiated.._param
Emp object serialized..

OUTPUT DE-SERIALIZATION>>>


Employee instantiated..
Engineer object instantiated_param-constructor_org
OrgData: 102 | abcd
Deserialized emp obj: Emp: null | 0 | null |transientVar 0 |empId 0

No comments:

Post a Comment

Thanks for your comments/Suggestions.