public abstract class InputStreamConsumer
extends java.lang.Thread
InputStream
until encountering the end of the stream or an
exception is thrown. If my finished-channel is not null
, I offer
myself on it before I die.
I check my interrupted status when I can, but I will usually not see that
I have been interrupted since I will almost always be blocked reading my
InputStream
which is not an interruptible channel. So, the best
way to get me to die is to close the InputStream
I am reading.
Modifier and Type | Field and Description |
---|---|
protected java.lang.Throwable |
exception |
protected java.util.concurrent.BlockingQueue<InputStreamConsumer> |
finishedChannel |
protected java.io.InputStream |
stream |
Constructor and Description |
---|
InputStreamConsumer()
Creates an instance.
|
InputStreamConsumer(java.lang.String name)
Creates an instance with the specified thread name.
|
InputStreamConsumer(java.lang.ThreadGroup group)
Creates an instance with the specified thread group.
|
InputStreamConsumer(java.lang.ThreadGroup group,
java.lang.String name)
Creates an instance with the specified thread group and thread name.
|
InputStreamConsumer(java.lang.ThreadGroup group,
java.lang.String name,
long stackSize)
Creates an instance with the specified thread group, thread name, and
thread stack size.
|
Modifier and Type | Method and Description |
---|---|
abstract java.nio.ByteBuffer |
data()
Returns the data consumed from my input stream.
|
java.lang.Throwable |
exception()
Returns my exception.
|
java.util.concurrent.BlockingQueue<InputStreamConsumer> |
finishedChannel()
Returns the finished-channel I use to signal that I have finished
running.
|
void |
finishedChannel(java.util.concurrent.BlockingQueue<InputStreamConsumer> newFinishedChannel)
Sets the finished-channel I use to signal that I have finished running.
|
boolean |
hasException()
Tests whether I have an exception.
|
protected void |
initialize()
Initializes me.
|
protected void |
postProcessData()
Performs post-processing of data, and is invoked from my
run()
method after a call to read() returns indicating
end-of-stream. |
protected abstract int |
read()
Reads a chunk of data from my input stream.
|
void |
run() |
java.io.InputStream |
stream()
Returns the input stream I read from.
|
void |
stream(java.io.InputStream newStream)
Sets the input stream I read from.
|
activeCount, checkAccess, clone, 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
protected java.io.InputStream stream
protected java.util.concurrent.BlockingQueue<InputStreamConsumer> finishedChannel
protected volatile java.lang.Throwable exception
public InputStreamConsumer()
super()
.public InputStreamConsumer(java.lang.String name)
super(name)
.name
- thread namepublic InputStreamConsumer(java.lang.ThreadGroup group)
super(group, (Runnable)null)
.group
- thread grouppublic InputStreamConsumer(java.lang.ThreadGroup group, java.lang.String name)
super(group, name)
.group
- thread groupname
- thread namepublic InputStreamConsumer(java.lang.ThreadGroup group, java.lang.String name, long stackSize)
super(group, null, name, stackSize)
.group
- thread groupname
- thread namestackSize
- thread stack sizeprotected void initialize()
public void run()
run
in interface java.lang.Runnable
run
in class java.lang.Thread
public java.util.concurrent.BlockingQueue<InputStreamConsumer> finishedChannel()
It is only safe to invoke this method before I have been started.
null
if one has not been setpublic void finishedChannel(java.util.concurrent.BlockingQueue<InputStreamConsumer> newFinishedChannel)
It is only safe to invoke this method before I have been started.
newFinishedChannel
- new finished-channel or null
if nonepublic java.io.InputStream stream()
It is only safe to invoke this method before I have been started.
null
if one has not been setpublic void stream(java.io.InputStream newStream)
It is only safe to invoke this method before I have been started.
newStream
- new input stream or null
if nonepublic boolean hasException()
true
if I have an exception; false
otherwisepublic java.lang.Throwable exception()
null
if nonepublic abstract java.nio.ByteBuffer data()
This method can be called safely from another thread only after
ensuring all my actions happen-before the action of calling this
method from the other thread. This is the Java memory model
happens-before relationship. For example, after I have been
started, it would be safe to call this method from another thread after
it called my join
method.
protected abstract int read() throws java.io.IOException
java.io.IOException
- if an I/O error occursprotected void postProcessData()
run()
method after a call to read()
returns indicating
end-of-stream.
This method does nothing, but subclasses may override it to do
something.