HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Programming with I/O Streams: Part 3"
Volume: 4 Issue: 2, p. 18

	

Listing 1: TeeOutputStream.java

import java.io.* ; 


public class  TeeOutputStream
    extends OutputStream
{
  OutputStream tee = null, out = null;


  public TeeOutputStream(OutputStream chainedStream, OutputStream teeStream)
  {
    out = chainedStream;

    if (teeStream == null)
      tee = System.out;
    else
      tee = teeStream;
  }
 

  /**
   * Implementation for parent's abstract write method. This writes out the
   * passed in character to the both, the chained stream and "tee" stream.
   */
  public void write(int c) throws IOException
  {
    out.write(c);

    tee.write(c);
    tee.flush();
  }


  /**
   * Closes both, chained and tee, streams.
   */
  public void close() throws IOException
  {
    flush();

    out.close();
    tee.close();
  }


  /**
   * Flushes chained stream; the tee stream is flushed each time a character
   * is written to it.
   */
  public void flush() throws IOException
  {
    out.flush();
  }



  /** Test driver */
  public static void main(String args[]) throws Exception
  {
    FileOutputStream fos = new FileOutputStream("test.out");
    TeeOutputStream tos = new TeeOutputStream(fos, System.out);
    PrintWriter   pw = new PrintWriter(new OutputStreamWriter(tos));

    pw.println("Testing line 1");
    pw.println("Testing line 2");

    pw.close();
  }
}

Listing 2: CountReader.java

import java.io.* ;


public class  CountReader
    extends Reader
{
  Reader in = null;
  int read = 0, charCount=0, wordCount = 0, lineCount = 0;
  boolean whiteSpace = true;


  public CountReader (Reader r)
  {
    in = r;
  }
  
  
  /**
   * Implementation for parent's read method. Counts chars, words, and
   * lines.
   */
  public int read(char[] array, int off, int len) throws IOException
  {
    if (array == null)
      throw new IOException("Null array");

    // Do actual read
    read    = in.read(array, off, len);

    // Now count
    charCount += read; // Increment character count
    char c;

    for (int i=0; i < read; i++)
    {
      c = array[i];

      // Line count
      if (c == '\n')
        lineCount++;

      // Word count
      if (Character.isWhitespace(c))
       whiteSpace = true;
      else
      if (Character.isLetterOrDigit(c) && whiteSpace)
      {
       wordCount++;
       whiteSpace = false;
      }
    }

    return read;
  }


  public void close() throws IOException
  {
    in.close();
  }


  public int getCharCount() { return charCount; }
  public int getWordCount() { return wordCount; }
  public int getLineCount() { return lineCount; }



  /** Test driver */
  public static void main(String args[]) throws Exception
  {
    CountReader cr = new CountReader(new FileReader("CountReader.java"));
    char c[]    = new char[4096];
    int read    = 0;
 
    while ((read = cr.read(c, 0, c.length)) != -1)
      System.out.print(new String(c, 0, read));

    cr.close();

    System.out.println("\n\nRead chars: " + cr.getCharCount() +
               "\n   words: " + cr.getWordCount() +
               "\n   lines: " + cr.getLineCount());
  }

}




  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.