
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of serialization in java, explaining what it is, how it works, and its various applications. Topics covered include object serialization, the use of objectinputstream and objectoutputstream, and the serialization api. The document also includes examples of writing and reading serialized objects, as well as customizing serialization for specific classes.
Typology: Slides
1 / 32
This page cannot be seen from the preview
Don't miss anything!

























RMI (Remote Method Invocation)
Lightweight persistence
created by the new operator … exists … † destroyed by the Java VM’s garbage collector when the program no longer holds a reference to the object
5
7
ObjectInputStream(InputStream) Creates an ObjectInputStream that reads from the specified InputStream. readObject() Reads an object from the input stream; is the opposite of writeObject(). available() Returns the number of bytes that can be read without blocking. close() Closes the input stream. readInt() Reads a 32-bit int from the stream.
defaultReadObject()
Reads the nonstatic and nontransient fields of the current class from the stream, ensuring the standard encoding format. You can call this method only from the readObject() method of the class being decoded.
For more details consult the Java API.
8
10
ObjectInputStream must be constructed on another stream.
(Here, the objects were archived in a file, so the code constructs an ObjectInputStream on a FileInputStream.)
Use ObjectInputStream's readObject method to read the String and the Date objects from the file.
The objects must be read from the stream in the same order in which they were written. Note that the return value from readObject is an object that is cast to and assigned to a specific type.
FileInputStream in = new FileInputStream("theTime"); ObjectInputStream s = new ObjectInputStream(in); String today = (String)s.readObject(); Date date = (Date)s.readObject();
13
/** pstruct is an object containing fields of different types, e.g. String, double, etc.*/
obj_outs.writeObject(pstruct.product_id);
obj_outs.writeObject(pstruct.product_name);
obj_outs.writeObject(pstruct.product_description);
obj_outs.writeObject(new Double(pstruct.price));
obj_outs.writeObject(pstruct.currency);
obj_outs.writeObject(new Boolean(pstruct.soft_good_p));
obj_outs.writeObject(new Boolean(pstruct.for_sale_p));
obj_outs.writeObject(pstruct.terms_and_conditions);
data = b_outs.toByteArray();
obj_outs.close();
b_outs.close(); Docsity.com
14
/** at this point, byte buffer contains the serialized form of pstruct */
/** reconstruct pstruct as follows */
ByteArrayInputStream b_ins = new ByteArrayInputStream(data);
ObjectInputStream obj_ins = new ObjectInputStream(b_ins);
16
17
public class MySerializableClass implements Serializable { ... }
Differs from Serializable in :
the amount of control in the serialization process the extent of customizations
writeObject, readObject responsible for serializing only the immediate class.
Any serialization required by the superclasses is handled automatically.
Class { explicitly coordinate with its superclasses to serialize itself } implementing the Externalizable interface.
+ : Complete, explicit control of the serialization process.
! : Only the class identification of the object is automatically saved by the stream. The class is responsible for writing and reading its contents, and it must coordinate with its superclasses to do so (whether or not the state of superclasses and which fields are stored).
writeExternal() must encode the data of the object in a form and sequence supported by readExternal().
Does not support code versioning you must provide your own versioning approach.
Above methods are public security risk (a client may be able to write or read information in the object other than by using its methods).
20
readExternal(ObjectInput) The object implements this method to restore its contents by calling the methods of DataInput for primitive types and readObject() for objects, strings and arrays. writeExternal(ObjectOutput) The object implements this method to save its contents by calling the methods of DataOutput for its primitive values or calling the writeObject() method of ObjectOutput for objects, strings and arrays. For more details consult the Java API.