The ObjectInputStream Class – Java I/O: Part I

The ObjectInputStream Class

An ObjectInputStream is used to restore (deserialize) objects that have previously been serialized using an ObjectOutputStream. An ObjectInputStream must be chained to an InputStream, using the following constructor:

Click here to view code image

ObjectInputStream(InputStream in) throws IOException

For example, in order to restore objects from a file, an ObjectInputStream can be chained to a FileInputStream:

Click here to view code image

FileInputStream inputFile = new FileInputStream(“obj-storage.dat”);
ObjectInputStream inputStream = new ObjectInputStream(inputFile);

The readObject() method of the ObjectInputStream class is used to read the serialized state of an object from the stream:

Click here to view code image

final Object readObject() throws ClassNotFoundException, IOException

Note that the reference type of the returned object is Object, regardless of the actual type of the retrieved object, and can be cast to the desired type. Objects and values must be read in the same order as when they were serialized.

Serializable, non-transient data members of an object, including those data members that are inherited, are restored to the values they had at the time of serialization. For compound objects containing references to other objects, the constituent objects are read to re-create the whole object structure. In order to deserialize objects, the appropriate classes must be available at runtime. Note that new objects are created during deserialization, so that no existing objects are overwritten.

The class ObjectSerializationDemo in Example 20.6 serializes some objects in the writeData() method at (1), and then deserializes them in the readData() method at (2). The readData() method also writes the data to the standard output stream.

The writeData() method at (1) writes the following values to the output stream: an array of strings (strArray), a long value (num), an array of int values (intArray), a String object (commonStr) which is shared with the array strArray of strings, and an instance (oneCD) of the record class CD whose component fields are all serializable.

Duplication is automatically avoided when the same object is serialized several times. The shared String object (commonStr) is actually only serialized once. Note that the array elements and the characters in a String object are not written out explicitly one by one. It is enough to pass the object reference in the writeObject() method call. The method also recursively goes through the array of strings, strArray, serializing each String object in the array. The current state of the oneCD instance is also serialized.

The method readData() at (2) deserializes the data in the order in which it was written. An explicit cast is needed to convert the reference of a deserialized object to a subtype. Applying the right cast is of course the responsibility of the application. Note that new objects are created by the readObject() method, and that an object created during the deserialization process has the same state as the object that was serialized.