Object Streams

Objects are serialized by object output streams. They are deserialized by object input streams. These are instances of java.io.ObjectOutputStream and java.io.ObjectInputStream, respectively:

public class ObjectOutputStream extends OutputStream 
  implements ObjectOutput, ObjectStreamConstants 
public class ObjectInputStream extends InputStream 
  implements ObjectInput, ObjectStreamConstants

The ObjectOutput interface is a subinterface of java.io.DataOutput that declares the basic methods used to write objects and data. The ObjectInput interface is a subinterface of java.io.DataInput that declares the basic methods used to read objects and data. java.io.ObjectStreamConstants is an unimportant interface that merely declares mnemonic constants for “magic numbers” used in the object serialization. (A major goal of the object stream classes is shielding client programmers from details of the format used to serialize objects such as magic numbers.)

Although these classes are not technically filter output streams, since they do not extend FilterOutputStream and FilterInputStream, they are chained to underlying streams in the constructors:

public ObjectOutputStream(OutputStream out) throws IOException
public ObjectInputStream(InputStream in) throws IOException

To write an object onto a stream, you chain an object output stream to the stream, then pass the object to the object output stream’s writeObject() method:

public final void writeObject(Object o) throws IOException

For example:

try {
  Point p = new Point(34, 22);
  FileOutputStream fout = new FileOutputStream("point.ser");
  ObjectOutputStream oout = new ObjectOutputStream(fout);
  oout.writeObject(p);
  oout.close();
}
catch (Exception e) {System.err.println(e);}

Later, the object can be read back using the readObject() method of the ObjectInputStream class:

public final Object readObject() 
            throws OptionalDataException, ClassNotFoundException, IOException

For example:

try {
  FileInputStream fin = new FileInputStream("point.ser");
  ObjectInputStream oin = new ObjectInputStream(fin);
  Object o = oin.readObject();
  Point p = (Point) o;  
  oin.close();
}
catch (Exception e) {System.err.println(e);}

The reconstituted point has the same values as the original point. Its x is 34 and its y is 22, just like the Point object that was written. However, since readObject() is only declared to return an Object, you usually need to cast the deserialized object to a more specific type.

Both writeObject() and readObject() throw IOException for all the usual reasons an I/O operation can fail (disk filling up, network connection being severed, etc.). The readObject() method also throws OptionalDataException if the stream doesn’t appear to contain an object in the proper format or a ClassNotFoundException if a definition for the class of the object read from the input stream is not available in the current VM.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.131.142.80