Class java.io.ObjectOutputStream
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.io.ObjectOutputStream

java.lang.Object
   |
   +----java.io.OutputStream
           |
           +----java.io.FilterOutputStream
                   |
                   +----java.io.DataOutputStream
                           |
                           +----java.io.ObjectOutputStream

public class ObjectOutputStream
extends DataOutputStream
implements ObjectOutput
An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream. If the stream is a network socket stream, the objects can be reconsituted on another host or in another process.

The class of each object is encoded including the class name and signature of the class, the values of the object's fields and arrays, and the closure of any other objects referenced from the initial objects.

The method writeObject is used to write an object to the stream. Any object, including Strings and arrays, is written with writeObject. Multiple objects or primitives can be written to the stream. The objects must be read back from the corresponding ObjectInputstream with the same types and in the same order as they were written.

Primitive data types can also be written to the stream using the appropriate methods from DataOutputStream. Strings can also be written using the writeUTF method.

The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields. References to other objects (except in transient or static fields) cause those objects to be written also. Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written.

For example to write an object that can be read by the example in ObjectInputStream:

	FileOutputStream ostream = new FileOutputStream("t.tmp");
	ObjectOutputStream p = new ObjectOutputStream(ostream);
	p.writeInt(12345);
	p.writeObject("Today");
	p.writeObject(new Date());
	p.flush();
	ostream.close();
Classes that require special handling during the serialization and deserialization process or that should NOT be writable must implement special methods with these exact signatures:

private void readObject(java.io.ObjectInputStream stream)
    throws IOException, ClassNotFoundException; 
private void writeObject(java.io.ObjectOutputStream stream)
    throws IOException

The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObject method can restore it. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutputStream.

Serialization of an object can be prevented by implementing writeObject and readObject methods that throw the NoAccessException. The exception will be caught by the ObjectOutputStream and abort the serialization process.

See Also:
DataOutputStream, ObjectInputStream

Constructor Index

 o ObjectOutputStream(OutputStream)
Creates an ObjectOutputStream that writes to the specified OutputStream.

Method Index

 o annotateClass(Class)
Subclasses may implement this method to allow class data to be stored in the stream.
 o replaceObject(Object)
This hook will allow subclasses of ObjectOutputStream to substitute one object for another during serialization.
 o writeObject(Object)
Write the specified object to the ObjectOutputStream.

Constructors

 o ObjectOutputStream
  public ObjectOutputStream(OutputStream out) throws IOException
Creates an ObjectOutputStream that writes to the specified OutputStream. The stream header is written to the stream.
Throws: IOException
Any exception thrown by the underlying OutputStream.

Methods

 o writeObject
  public final void writeObject(Object obj) throws ClassMismatchException, MethodMissingException, IOException
Write the specified object to the ObjectOutputStream. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are written. Default serialization for a class can be overridden using the writeObject and the readObject methods as described above. Objects referenced by this object are written transitively so that a complete equivalent graph of objects can be reconstructed by an ObjectInputStream.

Exceptions are thrown for problems with the OutputStream and for classes that should not be serialized. All exceptions are fatal to the OutputStream, it is left in an indeterminate state and it is up to the caller to ignore or recover the stream state.

Throws: MethodMissingException
Either readObject or writeObject special method is missing.
Throws: ClassMismatchException
A Class in a stream contains unknown primitive datatypes.
Throws: IOException
Any exception thrown by the underlying OutputStream.
 o annotateClass
  protected void annotateClass(Class cl) throws IOException
Subclasses may implement this method to allow class data to be stored in the stream. By default this method does nothing. The corresponding method in ObjectInputStream is resolveClass. This method is called exactly once for each unique class in the stream. The class name and signature will have already been written to the stream. This method may make free use of the ObjectOutputStream to save any representation of the class it deems suitable (for example, the bytes of the class file). The resolveClass method in the corresponding subclass of ObjectInputStream must read and use any data or objects written by annotateClass. annotateClass is called only for normal classes. Arrays are not normal classes.
Throws: IOException
Any exception thrown by the underlying OutputStream.
 o replaceObject
  protected Object replaceObject(Object obj) throws IOException
This hook will allow subclasses of ObjectOutputStream to substitute one object for another during serialization. Either a complementary substitution must be made during deserialization or the substituted object must be compatible with every field where the reference will be stored. Objects whose type is not a subclass of the type of the field or array element abort the serialization by raising an exception and the object is not be stored.

This method is called only once when each object is first encountered. All subsequent references to the object will be redirected to the new object. This method should return the object to be substituted or the original object.

Null can be returned as the object to be substituted, but may cause NullReferenceException in classes that contain references to the original object since they may be expecting an object instead of null.

This object substitution mechanism is needed, for example, in the case of remote method invocation. Surrogate references to remote objects can use the special methods to serialize themselves. However, references to local server objects should be serialized as the surrogate object that will be needed on the remote client. The remote method invocation infrastructure would define a subclass to map these local objects into the appropriate remote reference.

Throws: IOException
Any exception thrown by the underlying OutputStream.

All Packages  Class Hierarchy  This Package  Previous  Next  Index