[TOC] [Prev] [Next]

Object Input Interfaces


Topics:

The InputObject Interface

The ObjectInput interface provides an abstract interface to object deserialization. It extends DataInput so those methods for reading primitive data types are accessible to this interface.


package java.io;
public interface ObjectInput extends DataInput {
	public Object readObject()
		throws ClassNotFoundException, IOException;
}


The InputObjectStream Class

Class ObjectInputStream implements object deserialization. It maintains the state of the stream including the set of objects already deserialized. Its methods control the traversal of objects to be deserialized and makes calls to the class specific readObject methods and to resolveClass and resolveObject methods as appropriate.


package java.io;
public class ObjectInputStream
	extends DataInputStream	
	implements ObjectInput
{
	public ObjectInputStream(InputStream in)
		throws IOException, StreamCorruptedException;

	public final Object readObject()
		throws MethodMissingException, ClassMismatchException,
			StreamCorruptedException, ClassNotFoundException,
			IOException

	public void registerValidation(ObjectInputValidation obj,
		int priority)
		throws NotActiveException, ObjectInvalidException;

	protected Class resolveClass(String classname)
		throws IOException, ClassNotFoundException;

	protected Object resolveObject(Object obj)
		throws IOException;
}


The ObjectInputStream constructor requires an InputStream. The constructor reads and verifies the header and version written by the corresponding ObjectOutputStream constructor.

The readObject method is used to deserialize an object from the stream. It reads from the stream to reconstruct an object. In the process it calls the resolveClass and resolveObject methods as well as class specific readObject methods. 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.

When an exception terminates the deserialization some cleanup may be required. Prior to the exception one or more objects may have been created. These objects may not be in a usable state since they are only part of a graph of objects. Normally these objects will be remain unreferenced and not be accessible. Cleanup of these objects may be necessary if some action was taken by a readObject method or a validateObject callback that would make those objects accessible outside of the graph. If an exception occurs the readObjectCleanup method, if it has been implemented, is called for each object created.

The registerValidation method can be called to request a callback when the entire graph has been restored but before the object is returned to the original caller of readObject. The order of validate callbacks can be controlled using the priority. Callbacks registered with higher values are called before those with lower values. The object to be validated must support the ObjectInputValidation interface and implement the validateObject method. It is only correct to register validations during a call to a class's readObject method.

The resolveClass method is called while a class is being deserialized, after the class name and fingerprint have been read. Subclasses may extend this method to read other information about the class written by the corresponding subclass of ObjectOutputStream. The method must find and return the class with the given name. The default implementation calls Class.forName. If the class cannot be found ClassNotFoundException should be thrown.

The resolveObject method is called just after an object has been deserialized but before the object has been assigned or returned. A subclass's implementation may return a substitute object that will be assigned or returned instead of the original. The object returned must be of a type that is consistent and assignable to every reference to the original object. All assignments are type checked.

The ObjectInputValidation Interface

This interface allows an object to be called when a complete graph of objects has been deserialized. If the object cannot be made valid it should throw the ObjectInvalidException.


package java.io;
public interface ObjectInputValidation {
	public void validateObject()
		throws ObjectInvalidException;
}


The readObject and readObjectCleanup Methods

These are the required signatures of the special methods that allow a class to control the serialization of its own fields and to perform any cleanup in the case of an exception.


private void readObject(ObjectInputStream stream)
	throws NoAccessException, IOException;
private void readObjectCleanup(ObjectInputStream stream)
	throws IOException;


Exceptions Thrown by ObjectInputStream 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.