edu.rice.cs.util
Class StreamRedirectThread

java.lang.Object
  extended by java.lang.Thread
      extended by edu.rice.cs.util.StreamRedirectThread
All Implemented Interfaces:
Runnable

public class StreamRedirectThread
extends Thread

StreamRedirectThread is a thread which copies its input to its output and terminates when it completes.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
private static int BUFFER_SIZE
           
private  boolean close
           
private  CompletionMonitor cm
          This completion monitor is used when a new input stream is set.
private  Reader in
           
private  boolean keepRunning
          When keepRunning flag is set to true, the thread will not terminate when the stream ends.
private  Writer out
           
private  boolean stop
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
StreamRedirectThread(String name, InputStream in, OutputStream out)
          Constructor
StreamRedirectThread(String name, InputStream in, OutputStream out, boolean close)
          Constructor
StreamRedirectThread(String name, InputStream in, OutputStream out, boolean close, boolean keepRunning)
          Constructor
StreamRedirectThread(String name, InputStream in, OutputStream out, boolean close, ThreadGroup tg)
          Constructor
StreamRedirectThread(String name, InputStream in, OutputStream out, boolean close, ThreadGroup tg, boolean keepRunning)
          Constructor
StreamRedirectThread(String name, InputStream in, OutputStream out, ThreadGroup tg)
          Constructor
StreamRedirectThread(String name, InputStream in, OutputStream out, ThreadGroup tg, boolean keepRunning)
          Constructor
 
Method Summary
 void run()
          Copy.
 void setInputStream(InputStream in)
          Set a new input stream.
 void setStopFlag()
          Tell the thread to stop copying.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

in

private Reader in

out

private final Writer out

BUFFER_SIZE

private static final int BUFFER_SIZE
See Also:
Constant Field Values

stop

private volatile boolean stop

close

private volatile boolean close

keepRunning

private volatile boolean keepRunning
When keepRunning flag is set to true, the thread will not terminate when the stream ends. Instead, it will wait until another input stream is provided using setInputStream(), and begin reading from that stream. To terminate the thread, use setStopFlag().


cm

private volatile CompletionMonitor cm
This completion monitor is used when a new input stream is set. The copying loop waits for a signal, which is set in setInputStream().

Constructor Detail

StreamRedirectThread

public StreamRedirectThread(String name,
                            InputStream in,
                            OutputStream out,
                            boolean close)
Constructor

Parameters:
name - thread name
in - stream to copy from
out - stream to copy to
close - true if the streams should be closed after copying has ended

StreamRedirectThread

public StreamRedirectThread(String name,
                            InputStream in,
                            OutputStream out,
                            ThreadGroup tg)
Constructor

Parameters:
name - thread name
in - stream to copy from
out - stream to copy to
tg - thread group for this thread

StreamRedirectThread

public StreamRedirectThread(String name,
                            InputStream in,
                            OutputStream out,
                            boolean close,
                            ThreadGroup tg)
Constructor

Parameters:
name - thread name
in - stream to copy from
out - stream to copy to
close - true if the streams should be closed after copying has ended
tg - thread group for this thread

StreamRedirectThread

public StreamRedirectThread(String name,
                            InputStream in,
                            OutputStream out)
Constructor

Parameters:
name - thread name
in - stream to copy from
out - stream to copy to

StreamRedirectThread

public StreamRedirectThread(String name,
                            InputStream in,
                            OutputStream out,
                            boolean close,
                            boolean keepRunning)
Constructor

Parameters:
name - thread name
in - stream to copy from
out - stream to copy to
close - true if the streams should be closed after copying has ended
keepRunning - true if the thread should keep running and not terminate when a stream ends

StreamRedirectThread

public StreamRedirectThread(String name,
                            InputStream in,
                            OutputStream out,
                            ThreadGroup tg,
                            boolean keepRunning)
Constructor

Parameters:
name - thread name
in - stream to copy from
out - stream to copy to
tg - thread group for this thread
keepRunning - true if the thread should keep running and not terminate when a stream ends

StreamRedirectThread

public StreamRedirectThread(String name,
                            InputStream in,
                            OutputStream out,
                            boolean close,
                            ThreadGroup tg,
                            boolean keepRunning)
Constructor

Parameters:
name - thread name
in - stream to copy from
out - stream to copy to
close - true if the streams should be closed after copying has ended
tg - thread group for this thread
keepRunning - true if the thread should keep running and not terminate when a stream ends
Method Detail

setInputStream

public void setInputStream(InputStream in)
Set a new input stream.

Parameters:
in - new input stream to read from

run

public void run()
Copy.

Specified by:
run in interface Runnable
Overrides:
run in class Thread

setStopFlag

public void setStopFlag()
Tell the thread to stop copying.