Object Serialization - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Object Serialization

Description:

... have to be wrapped on another stream. ... from the stream. Restores the ... object saved in the stream with the correspondingly named fields in the ... – PowerPoint PPT presentation

Number of Views:247
Avg rating:3.0/5.0
Slides: 10
Provided by: elizabeth97
Category:

less

Transcript and Presenter's Notes

Title: Object Serialization


1
Object Serialization
  • CIS 413 - Spring 2006

2
Object Serialization
  • ObjectInputStream and ObjectOutputStream classes
    read and write objects.
  • Thus reading and writing objects is a process
    called object serialization.
  • Object serialization is used for
  • Remote Method Invocation (RMI)
  • communication between objects via sockets
  • Lightweight persistence
  • The archival of an object for use in a later
    invocation of the same program.

3
How to Write an Object
  • FileOutputStream out
  • out new FileOutputStream("theTime")
  • ObjectOutputStream s
  • s new ObjectOutputStream(out)
  • s.writeObject("Today")
  • s.writeObject(new Date())
  • s.flush()
  • Serializes the String Today and the current Date.
  • ObjectOutputStreams have to be wrapped on another
    stream.
  • This code is serializing the objects to a file
    named theTime

4
The writeObject method
  • Serializes the specified object, traverses its
    references to other objects recursively, and
    writes them all.
  • Preserves relationships between objects
  • ObjectOutputStream implements the DataOutput
    interface
  • Has methods for writing primitive data types,
    such as writeInt, writeFloat, or writeUTF.
  • You can use these methods to write primitive data
    types to an ObjectOutputStream.
  • The writeObject method throws a
    NotSerializableException if it's given an object
    that is not serializable.
  • An object is serializable only if its class
    implements the Serializable interface.

5
Serializable Interface
  • Known as a marker interface
  • Has no methods or fields and serves only to
    identify the semantics of being serializable.
  • Classes that do not implement this interface will
    not have any of their state serialized or
    deserialized.
  • All subtypes of a serializable class are
    themselves serializable.

6
How to Read an Object
  • FileInputStream in
  • in new FileInputStream("theTime")
  • ObjectInputStream s
  • s new ObjectInputStream(in)
  • String today (String)s.readObject()
  • Date date (Date)s.readObject()
  • Deserializes the String Today and the Date.
  • ObjectInputStreams also have to be wrapped on
    another stream
  • We are reading in from the same file to which we
    wrote the objects

7
Improving Serialization
  • Obvious, the more attributes in a class, the more
    time it takes to write all the values
  • You may or may not want to read in all values
    again (especially the case with shared memory)
  • remove some attributes
  • only write the minimal set of attributes
  • To say that an attribute will NOT be written, use
    the keyword transient

8
Special Handling
  • Classes that require special handling during the
    serialization and deserialization process must
    implement special methods with these exact
    signatures
  • private void writeObject(java.io.ObjectOutputStrea
    m out) throws IOException
  • private void readObject(java.io.ObjectInputStream
    in) throws IOException, ClassNotFoundException

9
Special Handling
  • readObject(ObjectInputStream in)
  • Reads from the stream
  • Restores the classes fields.
  • May call in.defaultReadObject to invoke the
    default mechanism for restoring the object's
    non-static and non-transient fields.
  • The defaultReadObject method uses information in
    the stream to assign the fields of the object
    saved in the stream with the correspondingly
    named fields in the current object.
  • This handles the case when the class has evolved
    to add new fields.
  • writeObject
  • Save state
  • Write the individual fields to the
    ObjectOutputStream using the writeObject method
    or by using the methods for primitive data types
    supported by DataOutput.
Write a Comment
User Comments (0)
About PowerShow.com