|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Thread
uk.ac.starlink.util.PipeReaderThread
public abstract class PipeReaderThread
Thread which reads data from a pipe. Having got an instance
of this class, you can call its getOutputStream method to
acquire a stream to write into, and then implement its
doReading(java.io.InputStream)
method to process the data; this method runs
in the new thread.
Here is an example of using the class to count the bytes written down a stream:
PipeReaderThread reader = new PipeReaderThread() { protected void doReading( InputStream dataIn ) throws IOException { int i; while ( dataIn.read() >= 0 ) i++; System.out.println( i ); } }; reader.start(); OutputStream dataOut = reader.getOutputStream(); // write bytes down dataOut ... dataOut.close(); reader.finishReading();Other uses will look pretty similar, but just override doReading in different ways. Note that any exceptions thrown by doReading are caught and eventually thrown in the reader thread by finishReading. The same exception may also be thrown by the
write
method of the writer thread.
This class serves two purposes. Firstly it copes with IOExceptions
encountered during the read, and makes sure they get thrown at the
writing end of the pipe (doReading
is declared to
throw IOException
).
Secondly it shields the user from the implementation of the piped
connection.
Performance of the
PipedInputStream
/PipedOutputStream
is dismal - this class may be able to do better.
The current implementation uses a couple of drop-in Piped*Stream
replacements, but performance still isn't great - it may be possible
to do better in future. You can provide your own paired pipes
by overriding both getInputStream()
and getOutputStream()
.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class java.lang.Thread |
---|
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler |
Field Summary |
---|
Fields inherited from class java.lang.Thread |
---|
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY |
Constructor Summary | |
---|---|
PipeReaderThread()
Constructs a new reader thread. |
Method Summary | |
---|---|
protected abstract void |
doReading(java.io.InputStream dataIn)
This method should be implemented to consume all the bytes in the given input stream. |
void |
finishReading()
Waits until the doReading method has finished reading the bytes written down the output stream, and returns. |
protected java.io.InputStream |
getInputStream()
Returns the stream at the input end of the pipe. |
java.io.OutputStream |
getOutputStream()
Returns the stream at the output end of the pipe. |
void |
run()
Implements the thread's run method to invoke doReading, catching and saving IOExceptions. |
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 |
Constructor Detail |
---|
public PipeReaderThread() throws java.io.IOException
java.io.IOException
Method Detail |
---|
protected java.io.InputStream getInputStream()
public java.io.OutputStream getOutputStream()
public void run()
run
in interface java.lang.Runnable
run
in class java.lang.Thread
protected abstract void doReading(java.io.InputStream dataIn) throws java.io.IOException
dataIn
- stream which will supply bytes
java.io.IOException
- if any I/O error occurs; this exception will
be saved and thrown later by the finishReading methodpublic void finishReading() throws java.io.IOException
java.io.IOException
|
Copyright © 2004 CLRC: Central Laboratory of the Research Councils. All rights reserved. | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |