[TOC] [Prev] [Next]

Object Output Interfaces


Topics:

The ObjectOutput Interface

The ObjectOutput interface provides an abstract interface to object serialization. It extends DataOutput so those methods for writing primitive data types are included in the interface.


package java.io;
public interface ObjectOutput extends DataOutput {
	public void writeObject(Object obj)
		throws IOException;
}


The ObjectOutputStream Class

Class ObjectOutputStream implements object serialization. It maintains the state of the stream including the set of objects already serialized. Its methods control the traversal of objects to be serialized and makes calls to the class specific writeObject methods and to annotateClass and replaceObject methods as appropriate.


package java.io;
public class ObjectOutputStream 
	extends DataOutputStream
	implements ObjectOutput
{
	public ObjectOutputStream(OutputStream out)
		throws IOException;

	public final void writeObject(Object obj)
		throws ClassMismatchException, MethodMissingException,
			IOException;

	protected void annotateClass(Class cl)
		throws IOException;

	protected Object replaceObject(Object obj)
		throws IOException;
}


The ObjectOutputStream constructor requires an OutputStream. The constructor writes a header and version to the stream that will be read and verified by the corresponding ObjectInputStream constructor.

The writeObject method is used to serialize an object to the stream. The exceptions thrown reflect errors during the traversal or exceptions that occur on the underlying stream. If any exception is thrown, the underlying stream is left in an unknown and unusable state.

The annotateClass method is called while a Class is being serialized, after the class name and fingerprint have been written to the stream. Subclasses may extend this method and write other information to the stream about the class. This information must be read by the resolveClass method on a corresponding ObjectInputStream subclass.

The replaceObject method is called just prior to serializing an object for the first time. A subclass's implementation may return a substitute object that will be serialized instead of the original. All references in the stream to the original object will replaced by the substitute object.

The writeObject Method

The writeObject method allows a class to control the serialization of its own fields. Here is its signature:


private void writeObject(ObjectOutputStream stream)
	throws NoAccessException, IOException


Exceptions Thrown by ObjectOutputStream Methods

All exceptions thrown by serialization classes are subclasses of ObjectStreamException which is a subclass of IOException.



[TOC] [Prev] [Next]

rmi-comments@jse.East.Sun.COM
Copyright © 1996, Sun Microsystems, Inc. All rights reserved.