Tuesday, December 22, 2009

Resolving Objects in Deserialization

Resolving Objects in Deserialization

Customer has send the data as serialized object to us, where as I am not interested in all the data or the class which serialized, or I have to create a class in which I can add aditional data in it.


We could do this operation by fetching the object and handing over to some method to create a new Object from it. It is painstaking job, and will not produce nice implmentation approach


We can achive resolving object by creating sub-class for ObjectInputStream with overriding method resolveObject(Object). In this method, we can do our business related sutff and get the objected wanted while deserializing




Example



I have created sub-class MyObjectInputStream from ObjectInputStream and override the resolve method. Note: resolveObject() method will not be called until unless the resolveObject flag set to true by calling method enableResolveObject(true);. I am setting this value true in constructor of the MyObjectInputStream class.





import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class DeserializeTest {

public static class EmployeeBean implements Serializable
{
String name;
String designation;
}

private static class MyObjectInputStream extends ObjectInputStream
{
public MyObjectInputStream(InputStream in) throws IOException {
super(in);
enableResolveObject(true);
}

@Override
protected Object resolveObject(Object obj) throws IOException {
if (obj instanceof EmployeeBean) {
EmployeeBean new_name = (EmployeeBean) obj;
StringBuffer sb=new StringBuffer();
sb.append(new_name.name);
sb.append(" ");
sb.append(new_name.designation);
return sb;
}

return super.resolveObject(obj);
}
}

/**
* @param args
*/
public static void main(String[] args) throws Exception{
EmployeeBean emp=new EmployeeBean();
emp.name = "Krishna";
emp.designation="PMTS";
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream out= new ObjectOutputStream(bos);
out.writeObject(emp);

ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
MyObjectInputStream in= new MyObjectInputStream(bis);
Object obj=in.readObject();
System.out.println(obj);
}
}


The Output will be like below

Krishna PMTS



In the above program comment enableResolveObject(true); line and try, the output will be the Object reference of EmployeeBeen; Something like below

ObjectStreamClassTest$EmployeeBean@10b30a7



Note: Playing with ObjectInputStream ,ObjectOutputStream, and ObjectStreamClass, we can do lot and control the content of serialized stream.

Reactions:

0 comments:

Post a Comment

Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More