nl.knowledgeplaza.util.io
Class TeeOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by nl.knowledgeplaza.util.io.TeeOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class TeeOutputStream
extends java.io.OutputStream

This class mirrors output between two streams. One sample usage of this is: (with no regard to exceptions);



 // setup a log file 
 FileOutputStream fout = new FileOutputStream("datafile");

 // setup a TeeOutputStream for that file and standard out.
 TeeOutputStream teeOut = new TeeOutputStream(System.out, fout);

 // Wrap the output stream in a PrintStream for added functionality 
 PrintStream loggedOutput = new PrintStream(teeOut);

 // reset standard out to our new tee'd stream set
 System.setOut(loggedOutput);

 // print to both underlying output streams 
 System.out.println("Hello there");

 

See Also:
OutputStream

Constructor Summary
TeeOutputStream(java.io.OutputStream first, java.io.OutputStream second)
          The constructor takes two OutputStreams.
 
Method Summary
 void close()
          Close the underlying OutputStreams.
 void flush()
          Flush both underlying OutputStreams.
 void write(int i)
          Write the specified int to each of the underlying OutputStreams.
 
Methods inherited from class java.io.OutputStream
write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TeeOutputStream

public TeeOutputStream(java.io.OutputStream first,
                       java.io.OutputStream second)
The constructor takes two OutputStreams. It will mirror any calls to each OutputStream.

Parameters:
first - The first output stream
second - The second output stream
Method Detail

write

public void write(int i)
           throws java.io.IOException
Write the specified int to each of the underlying OutputStreams.

Specified by:
write in class java.io.OutputStream
Parameters:
i - The int to write to the underlying streams.
Throws:
java.io.IOException - If one of the underlying streams throws it.

flush

public void flush()
           throws java.io.IOException
Flush both underlying OutputStreams.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - If one of the underlying streams throws it.

close

public void close()
           throws java.io.IOException
Close the underlying OutputStreams.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException - If one of the underlying streams throws it.


Copyright © 2010 KnowledgePlaza. All Rights Reserved.