Wraps a stream, printing to standard out everything that is written to it. : Writer « File Input Output « Java






Wraps a stream, printing to standard out everything that is written to it.

      
/*
 * LoggingWriter.java Created Sep 7, 2010 by Andrew Butler, PSL
 */
//package prisms.util;

import java.io.Writer;

/** Wraps a stream, printing to standard out everything that is written to it. */
public class LoggingWriter extends Writer {
  private java.io.Writer theBase;

  private java.io.Writer theLog;

  /**
   * @param base
   *            The writer to wrap
   * @param logFile
   *            The name of the file to write to
   * @throws java.io.IOException
   *             If the file cannot be written to
   */
  public LoggingWriter(java.io.Writer base, String logFile)
      throws java.io.IOException {
    theBase = base;
    if (logFile != null)
      theLog = new java.io.BufferedWriter(new java.io.FileWriter(logFile));
  }

  /**
   * @return The writer wrapped by this logging writer
   */
  public java.io.Writer getBase() {
    return theBase;
  }

  @Override
  public void write(char[] cbuf, int off, int len) throws java.io.IOException {
    theBase.write(cbuf, off, len);
    if (theLog != null)
      theLog.write(cbuf, off, len);
    else
      System.out.print(new String(cbuf, off, len));
  }

  @Override
  public void flush() throws java.io.IOException {
    theBase.flush();
    if (theLog != null)
      theLog.flush();
    else
      System.out.flush();
  }

  @Override
  public void close() throws java.io.IOException {
    theBase.close();
    if (theLog != null)
      theLog.close();
    else
      System.out.println();
  }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Null Writer
2.String Buffer Writer
3.Provides Closable semantics ordinarily missing in a java.io.CharArrayWriter
4.A writer for char strings
5.Write the entire contents of the supplied string to the given writer. This method always flushes and closes the writer when finished.
6.Writes all characters from a Reader to a file using the default character encoding.
7.Reads characters available from the Reader and returns these characters as a String object.
8.Quick Writer
9.Writer that places all output on an {@link Appendable} target.