Serialization
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.io.NotSerializableException;
import java.util.Calendar;
import java.util.Date;
/*
* Rule
* No :1. The object to be persisted must implement the Serializable interface
* or inherit that implementation from its object hierarchy
*
* No :2. The object to be persisted must mark all nonserializable fields transient
*
*
*/
class Source2
{
public int datam = 10;
private String unserial;
public Source2()
{
unserial = "unserial";
}
public String getName()
{
return unserial;
}
}
class Source
{
protected String name;
protected Integer age = 50;
transient int data = 100;
transient String data1 = "String";
public static int numb = 100;
public Source(String name)
{
this.name = name;
}
public Source(String name, int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public Integer getAge()
{
return age;
}
public static void setValue(int n)
{
numb = n;
}
public void setName(String name)
{
this.name = name;
}
public void setAge(Integer age)
{
this.age = age;
}
public Date getTime()
{
System.out.println("Time : "+Calendar.getInstance().getTime());
return Calendar.getInstance().getTime();
}
/*private void writeObject(ObjectOutputStream out) throws IOException
{
System.out.println("1--------Super Super Class writing Object +++++++++++++++++++++++++++++++++++++++ ");
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
System.out.println("1-------Super Super Class reading Object --------------------------------------- ");
}*/
}
//------------------------------------------------------------------------------------
//
class AddBook extends Source implements Serializable
{
// This Object Version Control Serial Number is mainly for using flatted Object
// Every Changes in class(say method include or remove) will genenerate
// unique automatic serialversionUID from hashing. Which will mismatch while
// reading Object creates
// Ex :
// "java.io.InvalidClassException: addBook; local class incompatible:
// stream classdesc serialVersionUID = 5752147104571118221, local class serialVersionUID = 1123121435424576345"
//
// For that We need to hardcorded the serialversionUID so that any changes in serializable
// code won't affect while on reading....
// This Object Version Control .. ie Stops the automatic generating ID
//
public static final long serialVersionUID = 12345l;
private Integer phoneNo;
//transient public Source2 obj = new Source2();
public AddBook (String name)
{
super(name);
}
public AddBook (String name, int age, int phoneNo)
{
super(name,age);
this.phoneNo = phoneNo;
}
public int getNumber()
{
return phoneNo;
}
/*
* These methods are build-in methods which are private methods
* If the User need custom protocol then user can write in here
*
*/
private void writeObject(ObjectOutputStream out) throws IOException
{
out.defaultWriteObject();
System.out.println("2-------------Super Class writing Object +++ "+getName());
out.writeObject(name);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
in.defaultReadObject();
name = (String)in.readObject();
// age = (Integer)in.readInt();
System.out.println("2-------------Super Class reading Object ------- "+name);
//obj = new Source2();
}
}
//--------------------------------------------------------------------------------------------
class SubAddBook extends AddBook
{
public SubAddBook(String name, Integer age, Integer phoneNo)
{
super(name, age, phoneNo);
// While Reading Object this setName happen only when this class is
// serializable
super.setName("Kumar");
}
/* Here We are Stopping the Serialization upto the Level we needed
* By Default, if superclass serialized then automatically subclass
* will get serailized
*
* Here We throw new Exception which will make Not Serializable
*/
private void writeObject(ObjectOutputStream out) throws IOException
{
System.out.println("3--------------Sub Class writing Object +++++++++++++++++++++++++++++++++++++++ ");
// throw new NotSerializableException("Not Now ");
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
System.out.println("3-------------Sub Class reading Object --------------------------------------- ");
// throw new NotSerializableException("Not Now ");
}
}
/*
*/
class Serialtest
{
private FileInputStream fis;
private ObjectInputStream in;
private FileOutputStream fos;
private ObjectOutputStream out;
private AddBook one, two, def;
private SubAddBook sone;
public Serialtest()
{
fileInit();
addAddressBook();
flatObject();
}
public void fileInit()
{
try {
fos = new FileOutputStream("object.txt");
fis = new FileInputStream("object.txt");
System.out.println("File Created .......... ");
out = new ObjectOutputStream(fos);
in = new ObjectInputStream(fis);
}
catch( Exception i )
{
}
}
public void addAddressBook()
{
def = new AddBook("Default");
//one = new AddBook("Name1",30, 273432);
//two = new AddBook("Name2", 25, 213213);
//sone = new SubAddBook("Name3", 26, 87686876);
}
public void flatObject()
{
try {
out.writeObject(def);
//out.writeObject(one);
//out.writeObject(two);
//out.writeObject(sone);
}
catch(IOException i){
i.printStackTrace();
}
}
public void readObject()
{
try {
AddBook ret1 = (AddBook)in.readObject();
Thread.sleep(1000);
System.out.println(" Time Read "+ret1.getTime());
Thread.sleep(1000);
System.out.println(" Current Time : "+Calendar.getInstance().getTime());
System.out.println(" ret "+ret1.data+" ret1 "+ret1.data1);
System.out.println(" ret "+ret1.numb);
ret1.setValue(78);
System.out.println(" ret "+ret1.numb);
System.out.println(" Default : "+ret1.getName()+" Age "+ret1.getAge());
//System.out.println(" Source2 : "+(ret1.obj).datam+" Name : "+(ret1.obj).getName());
//System.out.println(" ret "+(ret1.obj).datam);
//System.out.println("Name : "+ret1.getName()+" Age : "+ret1.getAge());
//AddBook ret2 = (AddBook)in.readObject();
//System.out.println("Name : "+ret2.getName()+" Age : "+ret2.getAge());
//AddBook ret3 = (AddBook)in.readObject();
//System.out.println("Name : "+ret3.getName()+" Age : "+ret3.getAge());
}
catch(Exception i){
i.printStackTrace();
}
}
public static void main(String arg[])
{
Serialtest a = new Serialtest();
System.out.println("Retrieving ................");
a.readObject();
}
}
---------------------------------------------------------------------------
No comments:
Post a Comment