Object Streams

Objects are serialized by object output streams. They are deserialized by object input streams. These classes are instances of java.io.ObjectOutputStream and java.io.ObjectInputStream, respectively:

public class ObjectOutputStream extends OutputStream
 implements ObjectOutput, 
public class ObjectInputStream extends InputStream
 implements ObjectInput, ObjectStreamConstants

The ObjectOutput interface is a subinterface of java.io.DataOutput that adds methods to write objects. The ObjectInput interface is a subinterface of java.io.DataInput that adds methods to read objects. By extending DataInput and DataOutput, ObjectInput and ObjectOutput guarantee that their implementers are able to read and write primitive types like int and double, as well as objects. Since an object may contain fields of primitive types, anything that has to read or write the state of an object also has to be able to read or write the primitive fields the object contains.

java.io.ObjectStreamConstants is an unimportant interface that merely declares mnemonic constants for "magic numbers" used in the object serialization file format. A major goal of the object stream classes is shielding client programmers from these sorts of details about the format used to serialize objects.

ObjectOutputStream and ObjectInputStream are filter streams, and thus they are chained to underlying streams in their constructors:

public ObjectOutputStream(OutputStream out) throws IOException
public ObjectInputStream(InputStream in) throws IOException

To write an object onto a stream, pass the object to the ObjectOutputStream's writeObject( ) method:

public final void writeObject(Object o) throws IOException

For example, this code fragment chains an ObjectOutputStream to a FileOutputStream and writes a java.awt.Point object into that file:

Point p = new Point(34, 22);
FileOutputStream fout = new FileOutputStream("point.ser");
ObjectOutputStream oout = new ObjectOutputStream(fout);
oout.close( );

Later, the object can be read back using the readObject( ) method of the ObjectInputStream class:

public final Object readObject( )
 throws OptionalDataException, ClassNotFoundException, IOException

For example:

FileInputStream fin = new FileInputStream("point.ser");
ObjectInputStream oin = new ObjectInputStream(fin);
Object o = oin.readObject( );
Point p = (Point) o;
oin.close( );

The reconstituted Point has the same values as the original Point. Its x is 34 and its y is 22, just like the Point object that was written. However, since readObject( ) is only declared to return an Object, you usually need to cast the deserialized object to a more specific type.

Both writeObject( ) and readObject( ) tHRow IOException for all the usual reasons an I/O operation can fail (disk filling up, network connection being severed, etc.). There are also several object-serialization specific subclasses of IOException. For example, an InvalidClassException indicates that the data in the stream can't be matched to the corresponding class (for instance, because the version of the class that was serialized is not the same as the version of the class used in the VM deserializing the object). The readObject( ) method also throws a ClassNotFoundException if a definition for the class of the object read from the input stream is not available in the current VM.

Basic I/O

Introducing I/O

Output Streams

Input Streams

Data Sources

File Streams

Network Streams

Filter Streams

Filter Streams

Print Streams

Data Streams

Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O



Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers


Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text


The Java Communications API


The J2ME Generic Connection Framework


Character Sets

Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net