-
-
Save andrewkroh/50f83a579cccf2717e68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.io.PrintStream; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Distributes standard output and standard error | |
* streams to registered listeners. The default | |
* stdout and stderr are always included as listeners | |
* (they always function as usual). | |
*/ | |
public class StandardStreamDistributor | |
{ | |
private static final Object _instanceGuard = new Object(); | |
private static StandardStreamDistributor _instance = null; | |
private final PrintStream stdout; | |
private final PrintStream stderr; | |
private final List<OutputStream> stdoutListeners; | |
private final List<OutputStream> stderrListeners; | |
public static final StandardStreamDistributor getInstance() | |
{ | |
synchronized(_instanceGuard) | |
{ | |
if (_instance == null) | |
{ | |
_instance = new StandardStreamDistributor(); | |
} | |
} | |
return _instance; | |
} | |
/** | |
* Reset the stdout and stderr back | |
* to their original destinations. | |
*/ | |
public void reset() | |
{ | |
System.setOut(stdout); | |
System.setErr(stderr); | |
} | |
public boolean addStdoutListener(OutputStream stream) | |
{ | |
return stdoutListeners.add(stream); | |
} | |
public boolean addStderrListener(OutputStream stream) | |
{ | |
return stderrListeners.add(stream); | |
} | |
public boolean removeStdoutListener(OutputStream stream) | |
{ | |
return stdoutListeners.remove(stream); | |
} | |
public boolean removeStderrListener(OutputStream stream) | |
{ | |
return stderrListeners.remove(stream); | |
} | |
private StandardStreamDistributor() | |
{ | |
stdoutListeners = new ArrayList<OutputStream>(); | |
stderrListeners = new ArrayList<OutputStream>(); | |
// Save off original standard streams: | |
stdout = System.out; | |
stderr = System.err; | |
stdoutListeners.add(stdout); | |
stderrListeners.add(stderr); | |
// Route System.out and System.err to this | |
// class for distribution: | |
System.setOut(new PrintStream(new DistributorOutputStream(stdoutListeners))); | |
System.setErr(new PrintStream(new DistributorOutputStream(stderrListeners))); | |
} | |
/** | |
* Distributes data to a list of OutputStreams. | |
*/ | |
private static class DistributorOutputStream extends OutputStream | |
{ | |
public DistributorOutputStream(List<OutputStream> destinations) | |
{ | |
dest = destinations; | |
} | |
@Override | |
public void write(int i) throws IOException | |
{ | |
for (OutputStream stream : dest) | |
{ | |
stream.write(i); | |
} | |
} | |
private final List<OutputStream> dest; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment