Package com.dtolabs.rundeck.core.utils
Class ThreadBoundOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- com.dtolabs.rundeck.core.utils.ThreadBoundOutputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
public class ThreadBoundOutputStream extends java.io.FilterOutputStream
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 methodsbindSystemOut()
andbindSystemErr()
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 withinstallThreadStream(java.io.OutputStream)
, and removed withremoveThreadStream()
.
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); }
-
-
Constructor Summary
Constructors Constructor Description ThreadBoundOutputStream(java.io.OutputStream stream)
Create a ThreadBoundOutputStream with a particular OutputStream as the default write destination.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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
close()
void
flush()
static java.io.PrintStream
getOrigSystemErr(java.io.PrintStream out)
Get the original System.err PrintStream if the input is the bound versionstatic java.io.PrintStream
getOrigSystemOut(java.io.PrintStream out)
Get the original System.out PrintStream if the input is the bound versionjava.io.OutputStream
getSink()
Return the original OutputStream sinkjava.io.OutputStream
getThreadStream()
void
installThreadStream(java.io.OutputStream stream)
Install an outputstream for the current thread and any child threadsjava.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 tobindSystemOut()
.static void
unbindSystemOut()
Resets the System.out printstream to the original PrintStream prior to the last call tobindSystemOut()
.void
write(int i)
-
-
-
Method Detail
-
installThreadStream
public void installThreadStream(java.io.OutputStream stream)
Install an outputstream for the current thread and any child threads- Parameters:
stream
- the new output stream
-
removeThreadStream
public java.io.OutputStream removeThreadStream()
Remove the custom stream for the current thread.- Returns:
- thread bound OutputStream or null if none exists
-
getThreadStream
public java.io.OutputStream getThreadStream()
-
write
public void write(int i) throws java.io.IOException
- Overrides:
write
in classjava.io.FilterOutputStream
- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException
- Specified by:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.FilterOutputStream
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.FilterOutputStream
- Throws:
java.io.IOException
-
bindSystemOut
public static ThreadBoundOutputStream bindSystemOut()
Bind the System.out PrintStream to a ThreadBoundOutputStream and return it. If System.out is already bound, returns the pre-bound ThreadBoundOutputStream. The binding can be removed withunbindSystemOut()
.- Returns:
- A ThreadBoundOutputStream bound to System.out
-
getOrigSystemOut
public static java.io.PrintStream getOrigSystemOut(java.io.PrintStream out)
Get the original System.out PrintStream if the input is the bound version- Parameters:
out
- pass in the current value of System.out- Returns:
- the original System.out after binding, or the out parameter
-
unbindSystemOut
public static void unbindSystemOut()
Resets the System.out printstream to the original PrintStream prior to the last call tobindSystemOut()
. WARNING: you should only call unbindSystemOut if you know that no other threads are depending on the System.out to be bound.Use theremoveThreadStream()
method to remove any OutputStream bound to a particular thread with a ThreadBoundOutputStream.
-
bindSystemErr
public static ThreadBoundOutputStream bindSystemErr()
Bind the System.err PrintStream to a ThreadBoundOutputStream and return it. If System.err is already bound, returns the pre-bound ThreadBoundOutputStream. The binding can be removed withunbindSystemErr()
.- Returns:
- A ThreadBoundOutputStream bound to System.err
-
getOrigSystemErr
public static java.io.PrintStream getOrigSystemErr(java.io.PrintStream out)
Get the original System.err PrintStream if the input is the bound version- Parameters:
out
- pass in the current value of System.err- Returns:
- the original System.err after binding, or the out parameter
-
unbindSystemErr
public static void unbindSystemErr()
Resets the System.out printstream to the original PrintStream prior to the last call tobindSystemOut()
. WARNING: you should only call unbindSystemOut if you know that no other threads are depending on the System.out to be bound. Use theremoveThreadStream()
method to remove any OutputStream bound to a particular thread with a ThreadBoundOutputStream.
-
getSink
public java.io.OutputStream getSink()
Return the original OutputStream sink- Returns:
- the OutputStream
-
-