public class ThreadBoundOutputStream
extends java.io.FilterOutputStream
bindSystemOut()
and bindSystemErr()
allow easy replacement
of the System.out and System.err printstreams, and access to the ThreadBoundOutputStream at any time after doing so.
installThreadStream(java.io.OutputStream)
, and
removed with removeThreadStream()
. public static void main(String[] args) throws FileNotFoundException { //bind to System.out, or retrieve already bound instance final ThreadBoundOutputStream newout = ThreadBoundOutputStream.bindSystemOut(); //start another thread which redirect System.out to a file Thread t1 = new Thread(new Runnable(){ public void run() { try { newout.installThreadStream(new FileOutputStream("thread1.output")); method1("thread1"); //writes to the thread1.output file } catch (FileNotFoundException e) { } } }); t1.start(); method1("main"); //prints to original System.out } public static void method1(String thread) { //pre-existing code which writes to System.out System.out.println("This output will be written to the correct outputstream in thread: " + thread); }
Constructor and Description |
---|
ThreadBoundOutputStream(java.io.OutputStream stream)
Create a ThreadBoundOutputStream with a particular OutputStream as the default write destination.
|
Modifier and Type | Method and Description |
---|---|
static ThreadBoundOutputStream |
bindSystemErr()
Bind the System.err PrintStream to a ThreadBoundOutputStream and return it.
|
static ThreadBoundOutputStream |
bindSystemOut()
Bind the System.out PrintStream to a ThreadBoundOutputStream and return it.
|
void |
flush() |
java.io.OutputStream |
getSink()
Return the original OutputStream sink
|
java.io.OutputStream |
getThreadStream() |
void |
installThreadStream(java.io.OutputStream stream)
Install an outputstream for the current thread and any child threads
|
java.io.OutputStream |
removeThreadStream()
Remove the custom stream for the current thread.
|
static void |
unbindSystemErr()
Resets the System.out printstream to the original PrintStream prior to the last call to
bindSystemOut() . |
static void |
unbindSystemOut()
Resets the System.out printstream to the original PrintStream prior to the last call to
bindSystemOut() . |
void |
write(int i) |
public ThreadBoundOutputStream(java.io.OutputStream stream)
stream
- default sinkpublic void installThreadStream(java.io.OutputStream stream)
stream
- the new output streampublic java.io.OutputStream removeThreadStream()
public java.io.OutputStream getThreadStream()
public void write(int i) throws java.io.IOException
write
in class java.io.FilterOutputStream
java.io.IOException
public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.FilterOutputStream
java.io.IOException
public static ThreadBoundOutputStream bindSystemOut()
unbindSystemOut()
.public static void unbindSystemOut()
bindSystemOut()
.
WARNING: you should only call unbindSystemOut if you know that no other threads are depending on the System.out
to be bound.Use the removeThreadStream()
method to remove any OutputStream bound to a particular thread
with a ThreadBoundOutputStream.public static ThreadBoundOutputStream bindSystemErr()
unbindSystemErr()
.public static void unbindSystemErr()
bindSystemOut()
.
WARNING: you should only call unbindSystemOut if you know that no other threads are depending on the System.out
to be bound. Use the removeThreadStream()
method to remove any OutputStream bound to a particular
thread with a ThreadBoundOutputStream.public java.io.OutputStream getSink()