|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.io.OutputStream
java.io.FilterOutputStream
com.dtolabs.rundeck.core.utils.ThreadBoundOutputStream
public class ThreadBoundOutputStream
ThreadBoundOutputStream allows a different OutputStream to be used for the current Thread and any child threads when
necessary, otherwise the default OutputStream is used. This is useful for replacing System.err or System.out to
capture output in a multi-threaded system, allowing using different sink OutputStreams for each Thread (and their
sub-threads).
Convenience methods 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.
Setting the correct OutputStream should be done with installThreadStream(java.io.OutputStream)
, and
removed with removeThreadStream()
.
Example code which replaces System.out and sets a two different
FileOutputStreams as the sinks for System.out for multpile threads:
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); }
Field Summary |
---|
Fields inherited from class java.io.FilterOutputStream |
---|
out |
Constructor Summary | |
---|---|
ThreadBoundOutputStream(java.io.OutputStream stream)
Create a ThreadBoundOutputStream with a particular OutputStream as the default write destination. |
Method Summary | |
---|---|
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. |
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)
|
Methods inherited from class java.io.FilterOutputStream |
---|
close, flush, write, write |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ThreadBoundOutputStream(java.io.OutputStream stream)
stream
- default sinkMethod Detail |
---|
public 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 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()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |