sofya.base
Class StreamRedirector

java.lang.Object
  extended by sofya.base.StreamRedirector
All Implemented Interfaces:
java.lang.Runnable

public class StreamRedirector
extends java.lang.Object
implements java.lang.Runnable

Class which reads from an input stream and pipes the data to a specified output stream.

All input is immediately flushed, as it is possible (although uncommon) for a subject to be designed to act on input without waiting for a newline. Note however that all streams are buffered, so if input occurs while this thread is not running it will be stored, and will be written as a block before being flushed when the thread runs again. If there is no input to be read, the thread will simply sleep.

The thread will terminate automatically if either the input stream or output stream is closed, so it is not necessary to explicitly signal this thread when the subject terminates. It can however be manually terminated by calling the stop method.

The primary intent of this class is to provide a means for redirecting input on stdin to a subject class. It is also used to prevent deadlock when a subject is not instrumented.


Constructor Summary
StreamRedirector(java.io.InputStream source, java.io.OutputStream sink, boolean closeOutOnFinish, java.io.PrintStream stderrStream)
          Standard constructor, creates a buffered connection from the specified input stream to the specified output stream.
 
Method Summary
 void run()
          Pipes data from input stream to output stream.
 void stop()
          Stops the thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StreamRedirector

public StreamRedirector(java.io.InputStream source,
                        java.io.OutputStream sink,
                        boolean closeOutOnFinish,
                        java.io.PrintStream stderrStream)
Standard constructor, creates a buffered connection from the specified input stream to the specified output stream.

Parameters:
source - Input stream from which data will be redirected to the output stream.
sink - Output stream to which input stream data will be redirected.
closeOutOnFinish - Specifies whether the output stream should be closed if the end of the input stream is reached. This is true, for example, when redirecting input to a subject.
Method Detail

stop

public void stop()
Stops the thread.


run

public void run()
Pipes data from input stream to output stream. Loops reading the input stream and copying the data to the output stream until one of the following events occurs: Closure of the output stream is reported (not thrown) as an exception.

Specified by:
run in interface java.lang.Runnable