bcds.tools
Class ThreadedPrintStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by java.io.PrintStream
              extended by bcds.tools.ThreadedPrintStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable

public class ThreadedPrintStream
extends java.io.PrintStream

Implements a PrintStream that offers a separate "channel" (output stream) to each thread in a multithreaded program. In fact, an instance of this class acts a proxy to the real, user-provided output streams. Its intended purpose is to replace System.out and/or System.err.

The main ideas for this implementation come from: http://maiaco.com/articles/java/threadOut.php

EXAMPLE OF USAGE:

   ...
   public static void main(Strings...args) {
      System.setOut(new ThreadedPrintStream(System.out));
      ...
      Thread th = new Runnable() {
         ...
         public void run()
         {
            ... os = ... // some OutputStream instance.
            ((ThreadedPrintStream) (System.out)).setThisThreadOutput(os);
            ...
         }
      };
      ...
 }
 

Author:
Juan Segovia S.

Field Summary
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
ThreadedPrintStream(java.io.OutputStream original_stream)
          Creates a new instance that sends all output to original_stream.
 
Method Summary
 boolean checkError()
          Flushes the output stream and returns false (always).
 void clearError()
           
 void close()
           
 void flush()
           
 java.io.OutputStream getThisThreadOutput()
          Returns the output stream for the current thread.
 void setThisThreadOutput(java.io.OutputStream os)
          Sets the output stream for the calling thread.
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 
Methods inherited from class java.io.PrintStream
append, append, append, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, setError
 
Methods inherited from class java.io.FilterOutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadedPrintStream

public ThreadedPrintStream(java.io.OutputStream original_stream)
Creates a new instance that sends all output to original_stream. If this instance is shared among several threads (as would be the case if it is used as a System.out replacement, for example), each thread will also write to original_stream automatically until a call to setThisThreadOutput is called.

Method Detail

setThisThreadOutput

public void setThisThreadOutput(java.io.OutputStream os)
Sets the output stream for the calling thread. To be effective, this method must be called from the execution context of the interested thread, for example, from a thread's run method.


getThisThreadOutput

public java.io.OutputStream getThisThreadOutput()
Returns the output stream for the current thread. If not explicitly set, the default output stream is returned.


close

public void close()
Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.PrintStream

flush

public void flush()
Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.PrintStream

write

public void write(byte[] b,
                  int off,
                  int len)
Overrides:
write in class java.io.PrintStream

write

public void write(int b)
Overrides:
write in class java.io.PrintStream

checkError

public boolean checkError()
Flushes the output stream and returns false (always).

Overrides:
checkError in class java.io.PrintStream

clearError

public void clearError()
Overrides:
clearError in class java.io.PrintStream