Java serialization

860 views

Published on

Published in: Technology, Business
  • Be the first to comment

Java serialization

  1. 1. JOURNEY INTO THE WORLD OF JAVA: JAVA SERIALIZATION
  2. 2. public class Vehicle{ public String number = ""; public Vehicle(){ System.out.println("Vehicle::Vehicle()"); } }public class Car extends Vehicle implements Serializable{ public String mark = ""; public String model = ""; public Car(){ System.out.println("Car::Car()"); } public Car(String mark, String model){ System.out.println("Car::Car(String, String)"); this.mark = mark; this.model = model; } } Serializable
  3. 3. public class Vehicle{ public String number = ""; public Vehicle(){ System.out.println("Vehicle::Vehicle()"); } } Serializable
  4. 4. public class Car extends Vehicle implements Serializable{ public String mark = ""; public String model = ""; public Car(){ System.out.println("Car::Car()"); } public Car(String mark, String model){ System.out.println("Car::Car(String, String)"); this.mark = mark; this.model = model; } } Serializable
  5. 5. System.out.println("Creating..."); Car car = new Car("Zaz", "Forza"); car.number = "AA0001BP"; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); System.out.println("Serializing..."); objectOutputStream.writeObject(car); objectOutputStream.flush(); byteArrayOutputStream.flush(); objectOutputStream.close(); byteArrayOutputStream.close(); ByteArrayInputStream bais = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); System.out.println("Deserializing..."); Car car2 = (Car)ois.readObject(); System.out.println("car2.number=" + car2.number); System.out.println("car2.mark=" + car2.mark); System.out.println("car2.model=" + car2.model); Serializable
  6. 6. System.out.println("Creating..."); Car car = new Car("Zaz", "Forza"); car.number = "AA0001BP"; Serializable
  7. 7. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); System.out.println("Serializing..."); objectOutputStream.writeObject(car); objectOutputStream.flush(); byteArrayOutputStream.flush(); objectOutputStream.close(); byteArrayOutputStream.close(); Serializable
  8. 8. ByteArrayInputStream bais = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); System.out.println("Deserializing..."); Car car2 = (Car)ois.readObject(); System.out.println("car2.number=" + car2.number); System.out.println("car2.mark=" + car2.mark); System.out.println("car2.model=" + car2.model); Serializable
  9. 9. Creating... Vehicle::Vehicle() Car::Car(String, String) Serializing... Deserializing... Vehicle::Vehicle() car2.number= car2.mark=Zaz car2.model=Forza Serializable
  10. 10. Serializable. Secure private void writeObject(java.io.ObjectOutputStream stream) throws java.io.IOException; private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, ClassNotFoundException; private void java.io.ObjectInputStream.defaultWriteObject(java.io.ObjectOutputStream stream) throws java.io.IOException; private void java.io.ObjectInputStream.defaultReadObject(java.io.ObjectInputStream stream) throws java.io.IOException, ClassNotFoundException; public interface java.io.ObjectInputValidation { public void validateObject() throws InvalidObjectException; }
  11. 11. Serializable. Features public class ArrayList extends AbstractList implements List, Cloneable,Serializable { private transient Object elementData[]; private int size; ... } class List implements Serializable { List next; private static final ObjectStreamField[] serialPersistentFields = {new ObjectStreamField("next", List.class)} };
  12. 12. • Adding new fields to a class • Changing the fields from static to nonstatic • Changing the fields from transient to nontransient Serializable. Can Manage Automatically private static final long serialVersionUID = 3329999866918906824L;
  13. 13. private/public/protected Object readResolve() throws ObjectStreamException private/public/protected Object writeReplace() throws ObjectStreamException Serializable. Singleton
  14. 14. Serializable. Singleton public class Singleton implements Serializable { private static final long serialVersionUID = 8413502725007695828L; private static Singleton instance = null; public String name = ""; private Singleton(){}; public static Singleton getInstance(){ if (instance == null){ instance = new Singleton(); } return instance; } private Object readResolve() throws ObjectStreamException{ instance.name = this.name; return instance; } }
  15. 15. Serializable. Singleton private Object readResolve() throws ObjectStreamException{ instance.name = this.name; return instance; } }
  16. 16. Serializable. Singleton Singleton instance1 = Singleton.getInstance(); instance1.name = "instance1"; ............................................................................ System.out.println("Serializing..."); System.out.println("instance1.name:" + instance1.name); .............................................................................. objectOutputStream.writeObject(instance1); ............................................................................ instance1.name = "instance2"; ............................................................................ System.out.println("Deserializing..."); Singleton instance2 = (Singleton)ois.readObject(); if(instance1 == instance2){ System.out.println("instance1 == instance2"); }else{ System.out.println("instance1 != instance2"); } System.out.println("instance2.name:" + instance2.name);
  17. 17. Serializable. Singleton Singleton instance1 = Singleton.getInstance(); instance1.name = "instance1"; ............................................................................ System.out.println("Serializing..."); System.out.println("instance1.name:" + instance1.name); .............................................................................. objectOutputStream.writeObject(instance1); ............................................................................ instance1.name = "instance2";
  18. 18. Serializable. Singleton ............................................................................ System.out.println("Deserializing..."); Singleton instance2 = (Singleton)ois.readObject(); if(instance1 == instance2){ System.out.println("instance1 == instance2"); }else{ System.out.println("instance1 != instance2"); } System.out.println("instance2.name:" + instance2.name);
  19. 19. Serializable. Singleton Serializing... instance1.name:instance1 Deserializing... instance1 == instance2 instance2.name:instance1
  20. 20. Serializable. ENUM public enum Color { RED("red"),GREEN("green"),BLUE("blue"); private Color(String name){ this.name = name; } public String getName(){ return name; } private String name; }
  21. 21. Serializable. ENUM Color red1 = Color.RED;Color green1 = Color.GREEN;Color blue1 = Color.BLUE; ..................................... System.out.println("Serializing..."); objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(red1); objectOutputStream.writeObject(green1); objectOutputStream.writeObject(blue1); ..................................... System.out.println("Deserializing..."); Color red2 = (Color)ois.readObject(); Color green2 = (Color)ois.readObject(); Color blue2 = (Color)ois.readObject(); if(red1 == red2) System.out.println("red1 == red2"); if(green1 == green2) System.out.println("green1 == green2"); if(blue1 == blue2) System.out.println("blue1 == blue2");
  22. 22. Serializable. ENUM Color red1 = Color.RED; Color green1 = Color.GREEN; Color blue1 = Color.BLUE; ..................................... System.out.println("Serializing..."); objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(red1); objectOutputStream.writeObject(green1); objectOutputStream.writeObject(blue1);
  23. 23. Serializable. ENUM ..................................... System.out.println("Deserializing..."); Color red2 = (Color)ois.readObject(); Color green2 = (Color)ois.readObject(); Color blue2 = (Color)ois.readObject(); if(red1 == red2) System.out.println("red1 == red2"); if(green1 == green2) System.out.println("green1 == green2"); if(blue1 == blue2) System.out.println("blue1 == blue2");
  24. 24. Serializable. ENUM Serializing... Deserializing... red1 == red2 green1 == green2 blue1 == blue2
  25. 25. Serializable. ENUM writeObject writeReplace serialPersistentFields serialVersionUID
  26. 26. public interface Externalizable extends java.io.Serializable { void writeExternal(ObjectOutput out) throws IOException; void readExternal(ObjectInput in) throws IOException, ClassNotFoundException; } Externalizable
  27. 27. Externalizable. Performance public class ItemExt implements Externalizable{ private int fieldInt; private boolean fieldBoolean; private long fieldLong; private float fieldFloat; private double fieldDouble; private String fieldString; public ItemExt(){ this(0,true,0,0,0,""); } ..................................................
  28. 28. Externalizable. Performance public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(fieldInt); out.writeBoolean(fieldBoolean); out.writeLong(fieldLong); out.writeFloat(fieldFloat); out.writeDouble(fieldDouble); out.writeUTF(fieldString); } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { fieldInt = in.readInt(); fieldBoolean = in.readBoolean(); fieldLong = in.readLong(); fieldFloat = in.readFloat(); fieldDouble = in.readDouble(); fieldString = in.readUTF(); }
  29. 29. Externalizable. Performance public abstract class ContainerExt implements Externalizable{ protected List<ItemExt> items; public ContainerExt(){ items = new LinkedList<ItemExt>(); } public void addItem(ItemExt item){ items.add(item); } .................................................. }
  30. 30. /*ContainerExt1*/public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(items); } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { items = (List<ItemExt>)in.readObject(); } Externalizable. Performance
  31. 31. Externalizable. Performance /*ContainerExt2*/ public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(items.size()); for(Externalizable ext : items) out.writeObject(ext); } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { int count = in.readInt(); for(int i=0; i<count; i++){ ItemExt ext = (ItemExt)in.readObject(); items.add(ext); } }
  32. 32. /*ContainerExt3*/ public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(items.size()); for(Externalizable ext : items) ext.writeExternal(out); } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException{ int count = in.readInt(); for(int i=0; i<count; i++){ ItemExt ext = new ItemExt(); ext.readExternal(in); items.add(ext); } } Externalizable. Performance
  33. 33. [java] Creating 100000 objects [java] Serializable: written in 971ms, readed in 1353 [java] Externalizable1: written in 1026ms, readed in 1129 [java] Externalizable2: written in 909ms, readed in 1876 [java] Externalizable3: written in 49ms, readed in 268 06/16/2013 08:24 PM 5,547,651 cont.ser 06/16/2013 08:24 PM 5,747,559 contExt1.ser 06/16/2013 08:24 PM 5,747,521 contExt2.ser 06/16/2013 08:24 PM 4,871,155 contExt3.ser Externalizable. Performance
  34. 34. https://github.com/MikhailGevak/Serialize

×