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;
}
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.
ObjectInvalidException.
package java.io;
public interface ObjectInputValidation {
public void validateObject()
throws ObjectInvalidException;
}
private void readObject(ObjectInputStream stream) throws NoAccessException, IOException; private void readObjectCleanup(ObjectInputStream stream) throws IOException;
ObjectStreamException which is a subclass of IOException.
ObjectStreamException - superclass of all serialization exceptions
ClassMismatchException - when the fingerprint of a class found locally does not match the fingerprint for the class in the stream or the class declaration contains unexpected field or method declarations.
MethodMissingException - thrown when only one of readObject and writeObject is found. If a class implements one it must implement the other.
NoAccessException - thrown by a readObject or writeObject method to terminate serialization or deserialization.
NotActiveException - thrown if registerValidation is not called during readObject.
ObjectInvalidException - thrown when a restored object cannot be made valid.
StreamCorruptedException - thrown when the stream header is invalid or when control information in the stream is not found or found to be invalid.