All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object
|
+----java.io.InputStream
|
+----java.io.FilterInputStream
|
+----java.io.DataInputStream
|
+----java.io.ObjectInputStream
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.
public ObjectInputStream(InputStream is) throws IOException, StreamCorruptedException
public final Object readObject() throws MethodMissingException, ClassMismatchException, StreamCorruptedException, ClassNotFoundException, IOException
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.
public synchronized void registerValidation(ObjectInputValidation obj,
int prio) throws NotActiveException, ObjectInvalidException
protected Class resolveClass(String classname) throws IOException, ClassNotFoundException
protected Object resolveObject(Object obj) throws IOException
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.
All Packages Class Hierarchy This Package Previous Next Index