Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 14
Externalization in Java :-
Before understanding Externalizable interface, you need to have idea about
Serialization. Java provides mechanism called serialization to persists java objects in a form of ordered or sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object. Externalizable: As name suggest it is externalilizing your serialization.If you want to customize your serialization mechanism then you can use it.It uses custom written mechanism to perform marshalling and unmarshalling of obects.Externalizable interface extends Serializable interface. If you implement this interface then you need to override following methods. !"verride public void readExternal(ObjectInput arg0) throws IOException,
Classot!oundException "
#
$Override public void writeExternal(ObjectOutput arg0) throws IOException "
# Now lets see how serialization happens: At sender side:- #$% chec&s if class implements externalizable or not.If it does then serialize obect using writeExternal'( method.If it does not implement externalizable but implements serializable , obect is serialized using "bect"utputStream. At receiver side: )hen obect is reconstructed and it is externalizable , an instance is created using no args constructor and readExternal is called.If it is not externalizable but serializable , obect is reconstructed using "bectInputStream. *ets start with example same as we have used in Serialization in ava. +reate Employee.ava in src,-com.arrams.externalizable E%plo&ee'java :( pac&age com.arrams.externalizable. import ava.io.Externalizable. import ava.io.I"Exception. import ava.io."bectInput. import ava.io."bect"utput. public class Employee implements Externalizable / int employeeId. String employee0ame. String department. String nationality. public Employee'( / 1 public int getEmployeeId'( / return employeeId. 1 public void setEmployeeId'int employeeId( / this.employeeId 2 employeeId. 1 public String getEmployee0ame'( / return employee0ame. 1 public void setEmployee0ame'String employee0ame( / this.employee0ame 2 employee0ame. 1 public String get3epartment'( / return department. 1 public void set3epartment'String department( / this.department 2 department. 1 public String get0ationality'( / return nationality. 1 public void set0ationality'String nationality( / this.nationality 2 nationality. 1 !"verride public void readExternal'"bectInput in( throws I"Exception, +lass0ot4oundException / employeeId 2 in.readInt'(. employee0ame 2 'String( in.read"bect'(. 1 !"verride public void writeExternal'"bect"utput out( throws I"Exception / out.writeInt'employeeId(. out.write"bect'employee0ame(. 1 1 &ou %ust have no args contructor i) &ou i%ple%ent externalizable' Create Eternalizable!ain.java in com.arrams.eternalizable ExternalizableMain.java: pac&age com.arrams.externalizable. import ava.io.4ileInputStream. import ava.io.4ile"utputStream. import ava.io.I"Exception. import ava.io."bectInputStream. import ava.io."bect"utputStream. public class Externalizable%ain / 566 6 !author Sai 7aghav 65 public static void main'String89 args( / Employee emp 2 new Employee'(. emp.setEmployeeId':;:(. emp.setEmployee0ame'<Sai 7aghav<(. emp.set3epartment'<+S<(. 55 Serialize try / 4ile"utputStream file"ut 2 new 4ile"utputStream'<employee.ser<(. "bect"utputStream outStream 2 new "bect"utputStream'file"ut(. outStream.write"bect'emp(. outStream.close'(. file"ut.close'(. 1 catch 'I"Exception i( / i.printStac&=race'(. 1 55 3eserialize emp 2 null. try / 4ileInputStream fileIn 2 new 4ileInputStream'<employee.ser<(. "bectInputStream in 2 new "bectInputStream'fileIn(. emp 2 'Employee( in.read"bect'(. in.close'(. fileIn.close'(. 1 catch 'I"Exception i( / i.printStac&=race'(. return. 1 catch '+lass0ot4oundException c( / System.out.println'<Employee class not found<(. c.printStac&=race'(. return. 1 System.out.println'<3eserialized Employee...<(. System.out.println'<Emp id> < ? emp.getEmployeeId'((. System.out.println'<0ame> < ? emp.getEmployee0ame'((. 1 1 *un it : )hen you run Externalizable%ain.ava.@ou will get following output 3eserialized Employee... Emp id> :;: 0ame> Sai 7aghav I) &ou alread& have serializable,wh& &ou need externalizable at all++: )hen you serialize any obect using serializable, apart from fields, all obects that belong to obect map and that can be reached using instance variable will also be serialized .for example > If you have Employee class and its superclass is person then it will serialize all superclass obects 'such as person( until it reaches <"bect< class. Similarly if Employee has instance variable of address class then it will serialize whole obect map of address also . 3o you really want this much overhead where all you want to serialize is employeeId and employee0ame #$% uses reflection when you use serializable which is Auite slow. )hile serializing,information about class description which includes description of its superclass and instance variable associated with that class also get stored in stream.Again this is also a performance issue. Inheritance in Externalization: 0ow we will see how inheritance affects externalization.So there can be multiple cases whether super class is externalizable or not.If not then how will you handle that and how it wor&s.*ets see by example. )e will create Berson.ava which will be superclass of Employee. Case ,: -hat i) super class does not i%ple%ent Externalizable: "f superclass does not implements eternalizable # you need to serialize superclass 's fields in subclass that implements Eternalizable. Create .erson class in co%'arra%s'externalizable'inheritence pac/age :( .erson'java :( pac/age co%'arra%s'externalizable'inheritence0 public class Berson / String name 2 <default<. String nationality. public Berson'( / System.out.println'<Berson>+onstructor<(. 1 public Berson'String name, String nationality( / super'(. this.name 2 name. this.nationality 2 nationality. 1 public String get0ame'( / return name. 1 public void set0ame'String name( / this.name 2 name. 1 public String get0ationality'( / return nationality. 1 public void set0ationality'String nationality( / this.nationality 2 nationality. 1 1 Create a E%plo&ee'java class in co%'arra%s'externalizable'inheritence pac/age :( E%plo&ee'java :( pac/age co%'arra%s'externalizable'inheritence0 i%port ava.io.Externalizable. i%port ava.io.I"Exception. i%port ava.io."bectInput. i%port ava.io."bect"utput. public class Employee extends Berson i%ple%ents Externalizable / int employeeId. String department. public Employee'( / 1 public Employee'int employeeId, String name, String department, String nationality( / super'name, nationality(. this.employeeId 2 employeeId. this.department 2 department. System.out.println'<Employee>+onstructor<(. 1 public int getEmployeeId'( / return employeeId. 1 public void setEmployeeId'int employeeId( / this.employeeId 2 employeeId. 1 public String get3epartment'( / return department. 1 public void set3epartment'String department( / this.department 2 department. 1 !"verride public void writeExternal'"bect"utput out( throws I"Exception / 56 6 since superclass does not implement externalizable, you need to 6 serialize super class field in this class itself 65 55 superclass fields out.write"bect'name(. out.write"bect'nationality(. 55 its own fields out.writeInt'employeeId(. out.write"bect'department(. 1 !"verride public void readExternal'"bectInput in( throws I"Exception, +lass0ot4oundException / 56 6 since superclass does not implement externalizable, you need to 6 deserialize super class field in this class itself 65 55 superclass fields name 2 'String( in.read"bect'(. nationality 2 'String( in.read"bect'(. 55 its own fields employeeId 2 in.readInt'(. department 2 'String( in.read"bect'(. 1 1 +reate Externalizable%ain.ava in the same pac&age >, Externalizable1ain'java :( pac/age co%'arra%s'externalizable'inheritence0 i%port ava.io.4ileInputStream. i%port ava.io.4ile"utputStream. i%port ava.io.I"Exception. i%port ava.io."bectInputStream. i%port ava.io."bect"utputStream. public class Externalizable%ain / 566 6 $author Sai 7aghav 65 public static void main'String89 args( / 55 Serialization Employee emp 2 new Employee':;:, <Sai 7aghav<, <+S<, <Indian<(. System.out.println'<Before serializing<(. System.out.println'<Emp id> < ? emp.getEmployeeId'((. System.out.println'<0ame> < ? emp.get0ame'((. System.out.println'<3epartment> < ? emp.get3epartment'((. System.out.println'<0ationality> < ? emp.get0ationality'((. System.out.println'<666666666666<(. System.out.println'<Serializing<(. tr& / 4ile"utputStream file"ut 2 new 4ile"utputStream'<employee.ser<(. "bect"utputStream outStream 2 new "bect"utputStream'file"ut(. outStream.write"bect'emp(. outStream.close'(. file"ut.close'(. 1 catch 'I"Exception i( / i.printStac&=race'(. 1 55 3eserialization System.out.println'<666666666666<(. System.out.println'<3eserializing<(. emp 2 null. tr& / 4ileInputStream fileIn 2 new 4ileInputStream'<employee.ser<(. "bectInputStream in 2 new "bectInputStream'fileIn(. emp 2 'Employee( in.read"bect'(. in.close'(. fileIn.close'(. 1 catch 'I"Exception i( / i.printStac&=race'(. return. 1 catch '+lass0ot4oundException c( / System.out.println'<Employee class not found<(. c.printStac&=race'(. return. 1 System.out.println'<After serializing<(. System.out.println'<Emp id> < ? emp.getEmployeeId'((. System.out.println'<0ame> < ? emp.get0ame'((. System.out.println'<3epartment> < ? emp.get3epartment'((. System.out.println'<0ationality> < ? emp.get0ationality'((. 1 1 *un it : )hen you run Externalizable%ain.ava.@ou will get following output> E%plo&ee:Constructor Before serializing Emp id> :;: 0ame> Sai 7aghav 3epartment> +S 0ationality> Indian 666666666666 Serializing 666666666666 3eserializing Berson>+onstructor After serializing Emp id> :;: 0ame> Sai 7aghav 3epartment> +S 0ationality> Indian Case 2: -hat i) super class i%ple%ents Externalizable: If superclass implements externalizable ,then it will also have readExternal'( and writeExternal'( method so it will serialize its own fields in these methods .erson'java :( pac&age com.arrams.externalizable.inheritence. import ava.io.Externalizable. import ava.io.I"Exception. import ava.io."bectInput. import ava.io."bect"utput. public class Berson implements Externalizable/ String name2<default<. String nationality. public Berson'( / System.out.println'<Berson>+onstructor<(. 1 public Berson'String name, String nationality( / super'(. this.name 2 name. this.nationality 2 nationality. 1 public String get0ame'( / return name. 1 public void set0ame'String name( / this.name 2 name. 1 public String get0ationality'( / return nationality. 1 public void set0ationality'String nationality( / this.nationality 2 nationality. 1 !"verride public void writeExternal'"bect"utput out( throws I"Exception / out.write"bect'name(. out.write"bect'nationality(. 1 !"verride public void readExternal'"bectInput in( throws I"Exception, +lass0ot4oundException / name2'String( in.read"bect'(. nationality2'String( in.read"bect'(. 1 1 E%plo&ee'java :, pac&age com.arrams.externalizable.inheritence. import ava.io.Externalizable. import ava.io.I"Exception. import ava.io."bectInput. import ava.io."bect"utput. public class Employee extends Berson implements Externalizable/ int employeeId. String department. public Employee'( / 1 public Employee'int employeeId,String name,String department,String nationality( / super'name,nationality(. this.employeeId2employeeId. this.department2department. System.out.println'<Employee>+onstructor<(. 1 public int getEmployeeId'( / return employeeId. 1 public void setEmployeeId'int employeeId( / this.employeeId 2 employeeId. 1 public String get3epartment'( / return department. 1 public void set3epartment'String department( / this.department 2 department. 1 !"verride public void writeExternal'"bect"utput out( throws I"Exception / super.writeExternal'out(. out.writeInt'employeeId(. out.write"bect'department(. 1 !"verride public void readExternal'"bectInput in( throws I"Exception, +lass0ot4oundException / super.readExternal'in(. employeeId2in.readInt'(. department2'String( in.read"bect'(. 1 1 create Externalizable%ain.ava in the same pac&age > Externalizable1ain'java :( pac&age com.arrams.externalizable.inheritence. import ava.io.4ileInputStream. import ava.io.4ile"utputStream. import ava.io.I"Exception. import ava.io."bectInputStream. import ava.io."bect"utputStream. public class Externalizable%ain / 566 6 !author Sai 7aghav 65 public static void main'String89 args( / 55Serialization Employee emp 2 new Employee':;:,<Sai 7aghav<,<+S<,<Indian<(. System.out.println'<Before serializing<(. System.out.println'<Emp id> < ? emp.getEmployeeId'((. System.out.println'<0ame> < ? emp.get0ame'((. System.out.println'<3epartment> < ? emp.get3epartment'((. System.out.println'<0ationality> < ? emp.get0ationality'((. System.out.println'<666666666666<(. System.out.println'<Serializing<(. try / 4ile"utputStream file"ut 2 new 4ile"utputStream'<employee.ser<(. "bect"utputStream outStream 2 new "bect"utputStream'file"ut(. outStream.write"bect'emp(. outStream.close'(. file"ut.close'(. 1catch'I"Exception i( / i.printStac&=race'(. 1 553eserialization System.out.println'<666666666666<(. System.out.println'<3eserializing<(. emp 2 null. try / 4ileInputStream fileIn 2new 4ileInputStream'<employee.ser<(. "bectInputStream in 2 new "bectInputStream'fileIn(. emp 2 'Employee( in.read"bect'(. in.close'(. fileIn.close'(. 1catch'I"Exception i( / i.printStac&=race'(. return. 1catch'+lass0ot4oundException c( / System.out.println'<Employee class not found<(. c.printStac&=race'(. return. 1 System.out.println'<After serializing<(. System.out.println'<Emp id> < ? emp.getEmployeeId'((. System.out.println'<0ame> < ? emp.get0ame'((. System.out.println'<3epartment> < ? emp.get3epartment'((. System.out.println'<0ationality> < ? emp.get0ationality'((. 1 1 *un it :( If you run Externalize%ain.ava you will get output as >, Employee>+onstructor $efore serializin% Emp id:&'& Name: (ai )a%hav *epartment: C( Nationality: "ndian ++++++++++++ (erializin% ++++++++++++ *eserializin% ,erson:Constructor -fter serializin% Emp id: &'& Name: (ai )a%hav *epartment: C( Nationality: "ndian In this example, since the Berson class stores and restores its fields in its own writeExternal and readExternal methods, you dont need to save5restore the superclass fields in sub class but if you observe closely the writeExternal and readExternal methods of Employee class, you will find that you still need to first call the super.xxxx'( methods that confirms the statement the externalizable obect must also coordinate with its supertype to save and restore its state. 3own4ides o) Externalizable: If you ma&e any change to your class definition, you need to maintain writeExternal'( and readExternal'( accordingly. As we have seen in example,Sub class obect has to coordinate with its superclass to save and store its state'by call super.xxxx'( method from subclass(