Object Streams

   

When Sun added Remote Method Invocation (RMI) to Java, it also added the capability to stream arbitrary objects. The ObjectInput and ObjectOutput interfaces define methods for reading and writing any object, in the same way that DataInput and DataOutput define methods for reading and writing primitive types. In fact, the ObjectInput and ObjectOutput interfaces extend the DataInput and DataOutput interfaces, respectively. The ObjectInput interface adds a single input method for reading in an object:

 public abstract Object readObject() throws ClassNotFoundException, IOException 

Similarly, the ObjectOutput interface adds a single output method:

 public abstract void writeObject(Object obj) throws IOException 

The two interfaces have a few other methods for dealing with the stream. These other methods define behavior that classes that implement these two interfaces must provide. The other methods on these two interfaces deal with determining the number of bytes available, reading and writing bytes, and flushing and closing the stream.

The ObjectOutputStream implements a stream filter that enables you to write any object to a stream, as well as any primitive type. Like most stream filters, you create an ObjectOutputStream by passing it an OutputStream :

 public OutputStream(OutputStream outStream) 

You can use the writeObject method to write any object to the stream:

 public final void writeObject(Object ob) 

The writeObject method can throw a ClassMismatchException, MethodMissingException, or IOException.

Because the ObjectOutputStream is a subclass of DataOutputStream, you can also use any of the methods from the DataOutput interface, such as writeInt or writeUTF.

Listing 21.10 shows a program that uses writeObject to stream a date and hash table to a file.

Listing 21.10 Source Code for WriteObject.java
 import java.io.*; import java.util.*; // This class writes out a date object and a hash table object // to a file called "writeme" using an ObjectOutputStream. public class WriteObject {   public static void main( String[] args )   {     // Create a hash table with a few entries     Hashtable writeHash = new Hashtable();     writeHash.put("Leader", "Moe");     writeHash.put("Lieutenant", "Larry");     writeHash.put("Stooge", "Curly");     try     {       // Create an output stream to a file called "writeobject.txt"       FileOutputStream fileOut = new FileOutputStream( "writeobject.txt" );       // Open an output stream filter on the file stream       ObjectOutputStream objOut = new ObjectOutputStream( fileOut );       // Write out the current date and the hash table       objOut.writeObject( new Date() );       objOut.writeObject( writeHash );       // Close the stream       objOut.close();      }      catch ( IOException ex )      {         ex.printStackTrace();      }    } } 

The ObjectInputStream, as you might have guessed, implements a stream filter for the ObjectInput interface. You create an ObjectInputStream by passing it the input stream you want it to filter:

 public ObjectInputStream(InputStream inStream) 

The readObject method reads an object from the input stream:

 public final Object readObject() throws MethodMissingException, ClassMismatchException      ClassNotFoundException, StreamCorruptedException, IOException 

You can also use any of the methods from the DataInput interface on an ObjectInputStream.

Listing 21.11 shows a program that uses readObject to read the objects written to the "writeobject.txt" file by the example in Listing 21.10.

Listing 21.11 Source Code for ReadObject.java
 import java.io.*; import java.util.*; // This class opens up the file "writeme" and reads two // objects from it. It makes no assumptions about the // types of the objects, it just prints them out. public class ReadObject extends Object {   public static void main(String[] args)   {     try     {       // Open an input stream to the file "writeme"       FileInputStream fileIn = new FileInputStream( "writeobject.txt" );       // Create an ObjectInput filter on the stream       ObjectInputStream objIn = new ObjectInputStream(fileIn);       // Read in the first object and print it       Object ob1 = objIn.readObject();       System.out.println(ob1);       // Read in the second object and print it       Object ob2 = objIn.readObject();       System.out.println(ob2);       // Close the stream        objIn.close();     }     catch( ClassNotFoundException ex )     {       ex.printStackTrace();     }     catch( IOException ex )     {       ex.printStackTrace();     }   } } 
   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

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