1 / 23

Serialization

Serialization. Celsina Bignoli bignolic@smccd.net. Definition. process of converting a graph of objects into a linear stream if bytes Used as persistence mechanism communication mechanism copy or manipulation mechanism

Download Presentation

Serialization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Serialization Celsina Bignoli bignolic@smccd.net

  2. Definition • process of converting a graph of objects into a linear stream if bytes • Used as • persistence mechanism • communication mechanism • copy or manipulation mechanism • the reverse operation, that takes the stream of data and makes it into a graph of objects is called deserialization • Used by RMI to pass objects between JVMs

  3. Serialization Client Server Original Instance Copy of Instance Stub Skeleton myMethod(instance of myObject) RMI Runtime RMI Runtime Network

  4. Copy mechanism • once an object is duplicated the two copies are completely independent from each other • the copy mechanism must create deep copies • if an object is sent twice, two separate copies will be created

  5. Serialization Implementation • To serialize an object: • create an instance of ObjectOutputStream • call the writeObject() method • To read a serialized object: • create an instance of ObjectInputStream • call the readObject() method

  6. ObjectOutputStream • defined in java.io package • implements the “writing-out” part of serialization • Has methods for • writing information to the stream • controlling stream’s behavior • customizing serialization mechanism

  7. “write” Methods • public void write(byte[] b); • public void write(byte[] b, int off, int len); • public void write(int data); • public void writeBoolean(boolean data); • public void writeByte(int data); • public void writeBytes(String data); • public void writeChar(int data); • public void writeChars(String data); • public void writeDouble(double data); • public void writeFields(); • public void writeFloat(float data); • public void writeInt(int data); • public void writeLong(long data); • public void writeObject(Object obj); • public void writeShort(short data); • public void writeUTF(String data); • public void defaultWriteObject();

  8. Stream Manipulation Methods • public void reset(); • public void close(); • public void flush(); • public void useProtocolVersion(int version);

  9. ObjectInputStream • defined in java.io package • implements the “reading-in” part of serialization • Has methods for • reading information from the stream • controlling stream’s behavior • customizing serialization mechanism

  10. “read” Methods • public int read(); • public int read(byte[] b, int off, int len); • public boolean readBoolean(); • buplic byte readByte(); • public char readChar(); • public double readDouble(); • public float readFloat(); • public intreadInt(); • public long readLong(); • public Object ReadObject(); • public short readShort(); • public byte readUnsignedByte(); • public short readUnsignedShort(); • public String readUTF(); • void defaultReadObject();

  11. Stream Manipulation Methods • public boolean available(); • public void close(); • public void readFully(byte[] data); • public void readFully(byte[] data, int offset, int size); • public int skipBytes(int len);

  12. How to Make a Class Serializable • implement the Serializable interface • make sure the instance-level, locally defined state is serialized properly • make sure the superclass state ius serialized properly • Override equals() and hashCode()if necessary

  13. How to Make a Class Serializable • locally defined variables are either • primitive types • serializable objects • non serializable object • declare them transient • implement writeObject() and readObject() • declare serialPersistentFields Ex: private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(“size”, Integer.TYPE) …)

  14. How to Make a Class Serializable • superclass must be • serializable • non-serializable • use serialPersistentFields • make sure superclass has a zero-argument constructor OR • use the Externalizable interface instead

  15. Serialization Algorithm • defines what gets written to the stream when an instance is serialized • defines a data format

  16. Inheritance Diagram The least superclass C1 (Object) C2 … CN The most-derived class Instance

  17. Write-out Information • description of the most derived class • data associated with the instance (as instance of the least superclass) • … • data associated with the instance (as instance of the most derived superclass)

  18. Class Metadata Information • Version Id of the class (ID used to validate .class file) • boolean to indicate weather writeObject()/readObject() are implemented • number of serializable fields • name and type of each field • extra data produced by annotateClass() method in ObjectOutputStream • codebase information, i.e. the location of class’s bytecode • a decription of its superclass if serializable

  19. Reading • ObjectInputStream gets the following information: • description of all the classes involved • Uses it to pull data from the stream • compare class description to local classes • if class has changed throws an exception • if the class has not changed, creates the instance and sets its state appropriately • serialization data from the instance

  20. Versioning Classes • discrepancy between metadata information sent through the wire and actual class implementation • because class hierarchy has changed • because of local changes to a serializable class

  21. Detecting class changes • Serialization creates a hashcode (of type long) from: • class name and modifiers • name of the interfaces the class implements • description of methods and constructor (no private) • description of all fields that are not private, static, private transient

  22. Performance Issues • Dependence on Reflection • verbose data format • easy to send more data than required

  23. Externalizable Interface • public void readExternal(ObjectInput in); • public void writeExternal (ObjectOutput out); • stores all metadata (like Serializable) but only writes local instance information (unlike Serializable) • you must implement the above methods

More Related