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 any InputStream subclass will possess read(), skip(), available(), and close() methods with these signatures. However, this interface may be implemented by classes that aren’t subclasses of InputStream.

The ObjectOutput interface declares the following six methods, all of which ObjectOutputStream faithfully implements. Except for writeObject(), which has already been discussed in the context of object output streams, these methods should behave exactly as they do for all output streams:

public abstract void writeObject(Object o) throws IOException
public abstract void write(int b) throws IOException
public abstract void write(byte data[]) throws IOException
public abstract void write(byte[] data, int offset, int length)
  throws IOException
public abstract void flush() throws IOException
public abstract void close() throws IOException

ObjectInput and ObjectOutput extend DataInput and DataOutput. Thus, as well as the methods declared outright, classes that implement ObjectInput must provide these additional methods as well:

public abstract void readFully(byte data[]) throws IOException
public abstract void readFully(byte data[], int offset, int length) 
  throws IOException
public abstract int skipBytes(int n) throws IOException
public abstract boolean readBoolean() throws IOException
public abstract byte readByte() throws IOException
public abstract int readUnsignedByte() throws IOException
public abstract short readShort() throws IOException
public abstract int readUnsignedShort() throws IOException
public abstract char readChar() throws IOException
public abstract int readInt() throws IOException
public abstract long readLong() throws IOException
public abstract float readFloat() throws IOException
public abstract double readDouble() throws IOException
public abstract String readLine() throws IOException
public abstract String readUTF() throws IOException

Classes that implement ObjectOutput must provide these additional methods:

public abstract void write(int b) throws IOException
public abstract void write(byte[] data) throws IOException
public abstract void write(byte[] data, int offset, int length) 
  throws IOException
public abstract void writeBoolean(boolean v) throws IOException
public abstract void writeByte(int b) throws IOException
public abstract void writeShort(int s) throws IOException
public abstract void writeChar(int c) throws IOException
public abstract void writeInt(int i) throws IOException
public abstract void writeLong(long l) throws IOException
public abstract void writeFloat(float f) throws IOException
public abstract void writeDouble(double d) throws IOException
public abstract void writeBytes(String s) throws IOException
public abstract void writeChars(String s) throws IOException
public abstract void writeUTF(String s) throws IOException

As I noted back in Chapter 7, there’s a bit of asymmetry between the DataInput and DataOutput interfaces. DataOutput declares the three write() methods you expect to find in output streams, but DataInput does not declare the three corresponding read() methods you expect to find in input streams. This asymmetry does not carry over into the ObjectInput and ObjectOutput interfaces where ObjectInput has read() methods and ObjectOutput has write() methods.

By extending DataInput and DataOutput, ObjectInput and ObjectOutput guarantee that their implementers are able to read and write both objects and primitive types like int and double. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.111.134