org.gjt.lindfors.util
Class StreamReaderThread

java.lang.Object
  extended by java.lang.Thread
      extended by org.gjt.lindfors.util.StreamReaderThread
All Implemented Interfaces:
java.lang.Runnable

public class StreamReaderThread
extends java.lang.Thread

Generalized reader thread for input streams. This thread can be extended for implementation of input stream readers. It contains three methods (cleanup, handleIOException and handleInput) which are left to be overridden by the subclass to introduce thread specific behaviour.

Cleanup should be overridden if there exists any resources which need to be freed when the thread terminates. HandleIOException can be overridden to add thread specific implementation to situations where the thread has been interrupted by an I/O exception. Finally, handleInput should be overridden to process all the input this thread reads.

For more detailed documentation, refer to the Util Library Tutorial .

Since:
JDK1.1
Version:
$Revision: 1.8 $
Author:
Juha Lindfors

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected  int timeToLive
          Wait this many milliseconds for the thread to exit gracefully when stopReading has been called.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
StreamReaderThread()
          Constructs a new daemon thread.
StreamReaderThread(java.io.Reader input)
          Constructs a new daemon thread with default buffer size.
StreamReaderThread(java.io.Reader input, boolean isDaemon)
          Constructs a new thread with default buffer size.
StreamReaderThread(java.io.Reader input, int bufSize)
          Constructs a new daemon thread with a given buffer size.
 
Method Summary
 void cleanup()
          Override to perform necessary cleanup.
 int getBufferSize()
          Returns the size of the buffer used by this thread.
 java.io.Reader getInputStream()
          Returns the input stream of this thread.
 void handleInput(char[] c, int len)
          Override to handle the input data read form the stream.
 void handleIOException(java.io.IOException e)
          Override to handle the IO Exceptions thrown by the read operation.
 void run()
          The thread implementation.
 void setBufferSize(int bufferSize)
          Sets the buffer size for this thread.
 void setExitOnError(boolean exitOnError)
          Sets the thread behaviour for error handling.
 void setInputStream(java.io.Reader input)
          Sets the input stream for this thread.
 void stopReading()
          Stops the reader thread.
 
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

timeToLive

protected int timeToLive
Wait this many milliseconds for the thread to exit gracefully when stopReading has been called. If the thread is unable to stop by this time, force it to interrupt.

Constructor Detail

StreamReaderThread

public StreamReaderThread(java.io.Reader input,
                          int bufSize)
Constructs a new daemon thread with a given buffer size. This thread uses 'character mode', in other words, it does not wait for a line break to be read before calling handleInput method.

Parameters:
input - the stream used for reading
buffSize - size of the read buffer in chars

StreamReaderThread

public StreamReaderThread(java.io.Reader input)
Constructs a new daemon thread with default buffer size. This thread uses 'character mode', in other words, it does not wait for a line break to be read before calling handleInput method.

Parameters:
input - the stream used for reading

StreamReaderThread

public StreamReaderThread(java.io.Reader input,
                          boolean isDaemon)
Constructs a new thread with default buffer size. This thread can be either user or daemon thread.

Parameters:
input - the stream used for reading
isDaemon - true for creating daemon thread, false for user thread

StreamReaderThread

public StreamReaderThread()
Constructs a new daemon thread.

Method Detail

stopReading

public void stopReading()
Stops the reader thread.


setExitOnError

public void setExitOnError(boolean exitOnError)
Sets the thread behaviour for error handling. If this is set to true, this thread will terminate upon errors in reading the stream. If it is set to false, the thread will attempt to continue reading from the stream. In this case it is the responsibility of the handleIOException method to ensure that any errors received from the stream are corrected and the input stream is ready to be used again.

Parameters:
exitOnError - true to exit when errors occur, false to attempt to continue reading
See Also:
handleIOException

setBufferSize

public void setBufferSize(int bufferSize)
Sets the buffer size for this thread.

Parameters:
bufferSize - the size of the buffer in chars

getBufferSize

public int getBufferSize()
Returns the size of the buffer used by this thread.

Returns:
the size in characters

setInputStream

public void setInputStream(java.io.Reader input)
Sets the input stream for this thread.

Parameters:
input - the stream used for reading

getInputStream

public java.io.Reader getInputStream()
Returns the input stream of this thread.

Returns:
input stream used for reading

run

public void run()
The thread implementation. Reads input either in 'character mode', n characters at a time, or in 'line mode', blocking until a line break is received from the input stream.

HandleInput is called to process all the data received from the input stream. HandleIOException is called if any errors occur during the read operations. Cleanup gets called after the thread has finished reading the stream.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

cleanup

public void cleanup()
Override to perform necessary cleanup.


handleIOException

public void handleIOException(java.io.IOException e)
Override to handle the IO Exceptions thrown by the read operation.

Parameters:
e - the exception that was caught

handleInput

public void handleInput(char[] c,
                        int len)
Override to handle the input data read form the stream.

Parameters:
c - character array containing the data from the input stream
len - length of the data read