Posted By

rnavanee on 08/20/13


Tagged

java serializable externalizable javase


Versions (?)

Externalizable Example


 / Published in: Java
 

URL: http://www.dzone.com/links/r/optimizing_java_serialization_java_vs_xml_vs_json.html

Externalizable interface provides you an alternate to Serializable interface. In Externalizable, you specify how/what to persist.

Seralizable persists all non-transient elements using default serialize/deserialize implementation.

  1. package com.test.jvm;
  2.  
  3. import java.io.Externalizable;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInput;
  8. import java.io.ObjectInputStream;
  9. import java.io.ObjectOutput;
  10. import java.io.ObjectOutputStream;
  11.  
  12. public class ExternalizableTest {
  13.  
  14. public static void main(String[] args) throws IOException, ClassNotFoundException {
  15. MyTestObject testObj = new MyTestObject();
  16. testObj.setL(12345678L);
  17. testObj.setS("hello");
  18.  
  19. // Serialize object
  20. FileOutputStream fos = new FileOutputStream("obj.ser");
  21. oos.writeObject(testObj);
  22.  
  23. // Deserialize object
  24. FileInputStream fis = new FileInputStream("obj.ser");
  25. MyTestObject objRead = (MyTestObject)ois.readObject();
  26. System.out.println("objRead: s="+objRead.getS()+", l="+objRead.getL());
  27. }
  28.  
  29. }
  30.  
  31. class MyTestObject implements Externalizable {
  32. private String s;
  33. private long l;
  34.  
  35. public MyTestObject() {
  36. }
  37.  
  38. public String getS() {
  39. return s;
  40. }
  41.  
  42. public void setS(String s) {
  43. this.s = s;
  44. }
  45.  
  46. public long getL() {
  47. return l;
  48. }
  49.  
  50. public void setL(long l) {
  51. this.l = l;
  52. }
  53.  
  54. @Override
  55. public void writeExternal(ObjectOutput out) throws IOException {
  56. out.writeObject(this.s);
  57. out.writeLong(this.l);
  58. System.out.println("writeExternal complete");
  59. }
  60.  
  61. @Override
  62. public void readExternal(ObjectInput in) throws IOException,
  63. this.s = (String) in.readObject();
  64. this.l = in.readLong();
  65. System.out.println("readExternal complete");
  66. }
  67. }
  68.  
  69.  
  70.  
  71. /*
  72. #1:
  73. Output:
  74. writeExternal complete
  75. readExternal complete
  76. objRead: s=hello, l=12345678
  77.  
  78.  
  79. #2:
  80. If the no-arg constructor of MyTestObject is commented out, you'll get the below error:
  81. Exception in thread "main" java.io.InvalidClassException: com.test.jvm.MyTestObject; com.test.jvm.MyTestObject; no valid constructor
  82. at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:724)
  83. at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1744)
  84. at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340)
  85. at java.io.ObjectInputStream.readObject(ObjectInputStream.java:362)
  86. at com.test.jvm.ExternalizableTest.main(ExternalizableTest.java:27)
  87. Caused by: java.io.InvalidClassException: com.test.jvm.MyTestObject; no valid constructor
  88. at java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:482)
  89. at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:321)
  90. at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1117)
  91. at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:337)
  92. at com.test.jvm.ExternalizableTest.main(ExternalizableTest.java:22)
  93.  
  94. */

Report this snippet  

You need to login to post a comment.