home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / jgl_1_1 / jgl_1_1.exe / src / OutputStreamIterator.java < prev    next >
Encoding:
Java Source  |  1996-09-10  |  2.1 KB  |  93 lines

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. import java.io.OutputStream;
  7. import java.io.IOException;
  8.  
  9. /**
  10.  * An OutputStreamIterator is an output iterator that prints objects that are written
  11.  * to it. By default, it writes to the standard output stream System.out.
  12.  * <p>
  13.  * @version 1.1
  14.  * @author ObjectSpace, Inc.
  15.  */
  16.  
  17. public class OutputStreamIterator implements OutputIterator
  18.   {
  19.   OutputStream myStream;
  20.  
  21.   /**
  22.    * Construct myself to print all objects to the standard output stream, System.out.
  23.    */
  24.   public OutputStreamIterator()
  25.     {
  26.     myStream = System.out;
  27.     }
  28.  
  29.   /**
  30.    * Construct myself to print all objects to the specified PrintStream.
  31.    * @param stream The PrintStream.
  32.    */
  33.   public OutputStreamIterator( OutputStream stream )
  34.     {
  35.     myStream = stream;
  36.     }
  37.  
  38.   /**
  39.    * Construct myself to be associated with the same PrintStream as the specified iterator.
  40.    */
  41.   public OutputStreamIterator( OutputStreamIterator iterator )
  42.     {
  43.     myStream = iterator.myStream;
  44.     }
  45.  
  46.   /**
  47.    * Print the object to my OutputStream.
  48.    * @param object The object.
  49.    */
  50.   public void put( Object object )
  51.     {
  52.     String s = (object == null ? "null" : object.toString() );
  53.     int len = s.length();
  54.  
  55.     try
  56.       {
  57.       for( int i = 0 ; i < len ; i++ ) 
  58.         myStream.write( s.charAt( i ) );
  59.  
  60.       myStream.write( ' ' );
  61.       }
  62.     catch( IOException exception )
  63.       {
  64.       System.err.println( "Caught exception " + exception );
  65.       }
  66.     }
  67.  
  68.   /**
  69.    * Advance by one. This has no effect for an OutputStreamIterator.
  70.    */
  71.   public void advance()
  72.     {
  73.     // Do nothing.
  74.     }
  75.  
  76.   /**
  77.    * Advance by a specified amount. This has no effect for a OutputStreamIterator.
  78.    * @param n The amount to advance.
  79.    */
  80.   public void advance( int n )
  81.     {
  82.     // Do nothing.
  83.     }
  84.  
  85.   /**
  86.    * Return a clone of myself.
  87.    */
  88.   public Object clone()
  89.     {
  90.     return new OutputStreamIterator( this );
  91.     }
  92.   }
  93.