230 likes | 369 Views
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
E N D
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 • 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
Serialization Client Server Original Instance Copy of Instance Stub Skeleton myMethod(instance of myObject) RMI Runtime RMI Runtime Network
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
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
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
“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();
Stream Manipulation Methods • public void reset(); • public void close(); • public void flush(); • public void useProtocolVersion(int version);
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
“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();
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);
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
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) …)
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
Serialization Algorithm • defines what gets written to the stream when an instance is serialized • defines a data format
Inheritance Diagram The least superclass C1 (Object) C2 … CN The most-derived class Instance
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)
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
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
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
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
Performance Issues • Dependence on Reflection • verbose data format • easy to send more data than required
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