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

Class java.io.ObjectInputStream

java.lang.Object
   |
   +----java.io.InputStream
           |
           +----java.io.FilterInputStream
                   |
                   +----java.io.DataInputStream
                           |
                           +----java.io.ObjectInputStream

public class ObjectInputStream
extends DataInputStream
implements ObjectInput
An ObjectInputStream deserializes primitive data and objects previously written using a ObjectOutputStream. ObjectOutputStream and ObjectInputStream can provide an application with persistent storage for graphs of objects when used with a FileOutputStream and FileInputStream respectively. ObjectInputStream is used to recover those objects previously serialized. Other uses include passing objects between hosts using a socket stream or for marshaling and unmarshaling arguments and parameters in a remote communication system.

ObjectInputStream ensures that the types of all objects in the graph created from the stream exactly match the classes present in the Java Virtual Machine. Classes are loaded as required using the standard mechanisms.

The method readObject is used to read an object from the stream. Java's safe casting should be used to get the desired type. In Java, strings and arrays are objects and are treated as objects during serialization. When read they need to be cast to the expected type.

Primitive data types can be read from the stream using the appropriate method on DataInputStream.

The default deserialization mechanism for objects restores the contents of each field to the value and type when it was written. Fields declared as transient or static are ignored by the deserialization process. References to other objects cause those objects to be read from the stream as necessary. Graphs of objects are restored correctly using a reference sharing mechanism. New objects are always allocated when deserializing, which prevents existing objects from being overwritten.

Reading an object is analogous to running the constructors of a new object. Memory is allocated for the object, initialized to initial values, and the fields filled in from the stream. The object is restored starting with the fields of class java.lang.Object down the class hierarchy to the most specific class. For each class, the fields of the object are restored. Since the object state is being recovered from the stream, constructors for the new objects are NOT run.

For example to read from a stream as written by the example in ObjectOutputStream:

	FileInputStream istream = new FileInputStream("t.tmp");
	ObjectInputStream p = new ObjectInputStream(istream);
	int i = p.readInt();
	String today = (String)p.readObject();
	Date date = (Date)p.readObject();
	istream.close();
Classes that require special handling during the serialization and deserialization process or that should NOT be deserializable must implement special methods with these signatures:

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

The readObject method is responsible for reading and restoring the state of the object for its particular class using data written to the stream by the corresponding writeObject method. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is restored by reading data from the ObjectInputStream for the individual fields and making assignments to the appropriate fields of the object. Reading primitive data types is supported by DataInputStream.

Deserialization of an object can be prevented by implementing writeObject and readObject methods to throw a NoAccessException. The exception will be caught by the ObjectInputStream and abort the reading process.

See Also:
DataInputStream, ObjectOutputStream

Constructor Index

 o ObjectInputStream(InputStream)
Create an ObjectInputStream that reads from the specified InputStream.

Method Index

 o readObject()
Read an object from the ObjectInputStream.
 o registerValidation(ObjectInputValidation, int)
Register an object to be validated before the graph is returned.
 o resolveClass(String)
Subclasses may implement this method to allow classes to be fetched from an alternate source.
 o resolveObject(Object)
ResolveObject can be used to substitute objects during deserialization.

Constructors

 o ObjectInputStream
  public ObjectInputStream(InputStream is) throws IOException, StreamCorruptedException
Create an ObjectInputStream that reads from the specified InputStream. The stream header containing the magic number and version number are read from the stream and verified.
Throws: StreamCorruptedException
The version or magic number are incorrect.
Throws: IOException
An exception occurred in the underlying stream.

Methods

 o readObject
  public final Object readObject() throws MethodMissingException, ClassMismatchException, StreamCorruptedException, ClassNotFoundException, IOException
Read an object from the ObjectInputStream. 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 read. Default deserializing for a class can be overriden using writeObject and readObject methods as describe above. Objects referenced by this object are read transitively so that a complete equivalent graph of objects is reconstructed by readObject.

The root object is completly restored when all of its fields and the objects it references are completely restored. At this point the object validation callbacks are executed in order based on their registered priorities. The callbacks are registered by objects (in the readObject special methods) as they are individually restored. Exceptions are thrown for problems with the InputStream and for classes that should not be deserialized. All exceptions are fatal to the InputStream and leave it in an indeterminate state; it is up to the caller to ignore or recover the stream state.

Throws: ClassNotFoundException
Class of a serialized object cannot be found.
Throws: MethodMissingException
A required method is missing or has been invoked improperly.
Throws: ClassMismatchException
A Class signature does not match the class in the stream or the class contains unknown primitive datatypes.
Throws: StreamCorruptedException
Control information in the stream is inconsistent.
Throws: IOException
Any of the usual Input/Output related exceptions.
 o registerValidation
  public synchronized void registerValidation(ObjectInputValidation obj,
                                              int prio) throws NotActiveException, ObjectInvalidException
Register an object to be validated before the graph is returned. While similar to resolveObject these validations are called after the entire graph has been reconstituted. Typically, a readObject method will register the object with the stream so that when all of the objects are restored a final set of validations can be performed.
Parameters:
obj - the object to be receive the validation callback.
prio - controls the order of callbacks;zero is a good default. Use higher numbers to be called back earlier, lower numbers for later callbacks. Within a priority, callbacks are processed in no particular order.
Throws: NotActiveException
The stream is not currently reading objects so it is invalid to register a callback.
Throws: ObjectInvalidException
The validation object is null.
 o resolveClass
  protected Class resolveClass(String classname) throws IOException, ClassNotFoundException
Subclasses may implement this method to allow classes to be fetched from an alternate source. By default Class.forName is called. The corresponding method in ObjectOutputStream is annotateClass. This method will be invoked only once for each unique class in the stream. This method can be implemented by subclasses to use an alternate loading mechanism but must return a Class object. Once returned, the signature of the class object is compared to the signature in the stream. If there is a mismatch, the deserialization fails and an exception is raised. ResolveClass is called only for normal classes. Arrays are not normal classes.
Throws: ClassNotFoundException
If class of a serialized object cannot be found.
 o resolveObject
  protected Object resolveObject(Object obj) throws IOException
ResolveObject can be used to substitute objects during deserialization. This method is called after an object has been read but before it is returned from readObject. The default resolveObject method just returns the new object. A subclass can examine the object and if necessary create a suitable alternative.

For example, a Font object might be serialized as a FontName object. During readObject the FontName object would be passed to resolveObject. ResolveObject could locate the closest available Font and return it instead.

Throws: IOException
Any of the usual Input/Output exceptions.

All Packages  Class Hierarchy  This Package  Previous  Next  Index