|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.io.InputStream | +--java.io.FilterInputStream | +--sdsu.io.NonBlockingInputStream
This class input stream can be interrupted and be set to time-out if a read takes too long. Inputstreams in the JDK 1.1, 1.2 block. If you perform a read on a stream the method will not return until input data is available, the end of the stream is detected, or an exception is thrown. There are times when you would like to "cancel" a read request. All read requests on this stream can be "canceled" in two different ways. First, you can set a time-out duration. The time is measured in wall clock time. If a read request times out wating for any bytes to read the exception InterruptedIOException is thrown. Second, you can send the method Thread.interrupt() to the thread that requested the read. The read will then throw an InterruptedIOException. Regardless of how the read is "canceled" further read requests can be made on the stream.
Constructor Summary | |
NonBlockingInputStream(java.io.InputStream input)
Create a NonBlockingInputStream on the input stream with poll delay of 50 milliseconds and time-out length of about 200,000 centuries. |
|
NonBlockingInputStream(java.io.InputStream input,
int pollDelay,
long timeout)
Create a NonBlockingInputStream on the input stream the given poll delay time-out. |
Method Summary | |
int |
read()
Reads the next byte of data from the input stream. |
int |
read(byte[] inputBuffer,
int offset,
int length)
Reads up to length bytes of data from the input stream
into an array of bytes. |
void |
setPollDelay(int delay)
Set the poll delay. |
void |
setTimeout(long timeout)
Set the time-out period. |
Methods inherited from class java.io.FilterInputStream |
available,
close,
mark,
markSupported,
read,
reset,
skip |
Methods inherited from class java.lang.Object |
equals,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Constructor Detail |
public NonBlockingInputStream(java.io.InputStream input)
public NonBlockingInputStream(java.io.InputStream input, int pollDelay, long timeout)
pollDelay
- time in millisecondsMethod Detail |
public void setPollDelay(int delay) throws java.lang.IllegalArgumentException
delay
- time in milleseconds a read method will
sleep between ckecking the input for a byte of data.public void setTimeout(long timeout) throws java.lang.IllegalArgumentException
timeout
- time in milleseconds. If a byte of data does not
become available for reading in this time (wall time), a read method
will throw an InterruptedIOException.read()
public int read() throws java.io.IOException, java.io.InterruptedIOException
int
in the range
0
to 255
. If no byte is available
because the end of the stream has been reached, the value
-1
is returned.
This method polls the input for data. Polling continues
until input data is available,
end of file is detected, the time-out period is exceeded,
the current threads is interrupted via Thread.interrupt(), or an exception
is thrown.-1
if the end of the
stream is reached.public int read(byte[] inputBuffer, int offset, int length) throws java.io.IOException, java.io.InterruptedIOException
length
bytes of data from the input stream
into an array of bytes.
An attempt is made to read as many as length
bytes, but a smaller number may be read,
possibly zero. The number of bytes actually
read is returned as an integer.
This method polls the input for data. Polling continues until input data is available, end of file is detected, the time-out period is exceeded, the current threads is interrupted via Thread.interrupt(), or an exception is thrown.
If no byte is
available because the stream is at end of
file, the value -1
is returned.
The first
byte read is stored into element inputBuffer[offset]
,
the next one into inputBuffer[offset+1]
,
and so on. The number of bytes read is,
at most, equal to length
.
If the first byte cannot
be read for any reason other than end of
file, then an IOException
is
thrown. In particular, an IOException
is thrown if the input stream has been closed.
inputBuffer
- the buffer into which the data is read.offset
- the start offset of the data.length
- the maximum number of bytes read.-1
if there is no more data because the end of
the stream has been reached.InputStream.read()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |