O'Reilly logo

Java I/O by Elliotte Rusty Harold

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The ObjectInput and ObjectOutput Interfaces

As well as the ObjectInputStream and ObjectOutputStream classes, the java.io package also provides ObjectInput and ObjectOutput interfaces:

public interface ObjectInput extends DataInput 
public interface ObjectOutput extends DataOutput

These interfaces are not much used in Java 1.1 and 2. The only classes in the core API that actually implement them are ObjectInputStream and ObjectOutputStream. However, several methods used for customization of the serialization process are declared to accept ObjectInput or ObjectOutput objects as arguments, rather than specifically ObjectInputStream or ObjectOutputStream objects. This provides a little wiggle room for Java to grow in unforeseen ways.

The ObjectInput interface declares seven methods, all of which ObjectInputStream faithfully implements:

public abstract Object readObject() 
  throws ClassNotFoundException, IOException
public abstract int read() throws IOException
public abstract int read(byte[] data) throws IOException
public abstract int read(byte[] data, int offset, int length) 
  throws IOException
public abstract long skip(long n) throws IOException
public abstract int available() throws IOException
public abstract void close() throws IOException

The readObject() method has already been discussed in the context of object input streams. The other six methods behave exactly as they do for all input streams. In fact, at first glance, all these methods except readObject() appear superfluous, since ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required