/**
* SeDe
* Explained Cloning, Singleton and SeDe of the Emp instance.
*
* for readResolve() and writeReplace() method explanations look-
* http://docs.oracle.com/javase/1.3/docs/guide/serialization/spec/serialTOC.doc.html
*
*/
package com.sede;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
/**
* @author u2fv
*
*/
class Test {
}
class Emp implements Serializable, Cloneable {
private int eNo;
private String eName;
private transient float sal;
private Test test;
private static Emp emp;
private Emp() {
this.eNo = 0;
this.eName = null;
this.sal = 0.0f;
}
private Emp(int eNo, String eName, float sal) {
super();
this.eNo = eNo;
this.eName = eName;
this.sal = sal;
}
public static synchronized Emp getInstance() {
System.out.println("inside getInstance()");
if (emp == null) {
emp = new Emp();
}
return emp;
}
public static synchronized Emp getInstance(int i, String name, float sal) {
System.out.println("inside getInstance(1,2,3)");
if (emp == null) {
emp = new Emp(1, "rama", 3.4f);
}
return emp;
}
private Object readResolve() throws ObjectStreamException {
System.out.println("inside readResolve()");
return emp;
}
private Object writeReplace() throws ObjectStreamException {
System.out.println("inside writeReplace()" + " " + emp);
return emp;
}
@Override
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException(
"You have no privileges to clone Emp instance");
}
@Override
public String toString() {
return eNo + " " + eName + " " + sal;
}
}
class SeDe {
public void doSe(Emp emp) {
try {
FileOutputStream fOS = new FileOutputStream("Myfile.ser");
ObjectOutputStream oOS = new ObjectOutputStream(fOS);
oOS.writeObject(emp);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public Emp doDe(Emp emp) {
try {
FileInputStream fOS = new FileInputStream("Myfile.ser");
ObjectInputStream oOS = new ObjectInputStream(fOS);
emp = (Emp) oOS.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return emp;
}
public Emp doDeSecondTime(Emp emp) {
try {
FileInputStream fOS = new FileInputStream("Myfile.ser");
ObjectInputStream oOS = new ObjectInputStream(fOS);
emp = (Emp) oOS.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return emp;
}
}
public class TestSeDe {
/**
* @param args
*/
public static void main(String[] args) {
// Emp emp = Emp.getInstance();
Emp emp = Emp.getInstance(1, "rama", 2.3f);
SeDe seDe = new SeDe();
seDe.doSe(emp);
emp = null;
emp = seDe.doDe(emp);
System.out.println(emp + " || hashCode: " + emp.hashCode());
Emp emp1 = null;
emp1 = seDe.doDeSecondTime(emp1);
System.out.println(emp1 + " || hashCode: " + emp1.hashCode());
}
}
--------
No comments:
Post a Comment