All Packages Class Hierarchy This Package Previous Next Index
Class sdsu.io.NonBlockingInputStream
java.lang.Object
|
+----java.io.InputStream
|
+----java.io.FilterInputStream
|
+----sdsu.io.NonBlockingInputStream
- public class NonBlockingInputStream
- extends FilterInputStream
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.
- Version:
- 1.0 6 January 1999
- Author:
- Roger Whitney
(whitney@cs.sdsu.edu)
-
NonBlockingInputStream(InputStream)
- Create a NonBlockingInputStream on the input stream with poll delay
of 50 milliseconds and time-out length of about 200,000 centuries.
-
NonBlockingInputStream(InputStream, int, long)
- Create a NonBlockingInputStream on the input stream the given poll delay
time-out.
-
read()
- Reads the next byte of data from the input stream.
-
read(byte[], int, int)
- Reads up to
length
bytes of data from the input stream
into an array of bytes.
-
setPollDelay(int)
- Set the poll delay.
-
setTimeout(long)
- Set the time-out period.
NonBlockingInputStream
public NonBlockingInputStream(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
public NonBlockingInputStream(InputStream input,
int pollDelay,
long timeout)
- Create a NonBlockingInputStream on the input stream the given poll delay
time-out.
- Parameters:
- pollDelay - time in milliseconds
- Throws: IllegalArgumentException
- if pollDelay < 0 or timeout < pollDelay.
setPollDelay
public void setPollDelay(int delay) throws IllegalArgumentException
- Set the poll delay. This is the time the thread
will sleep between polling the input for a byte of data.
- Parameters:
- delay - time in milleseconds a read method will
sleep between ckecking the input for a byte of data.
- Throws: IllegalArgumentException
- if duration < 0.
setTimeout
public void setTimeout(long timeout) throws IllegalArgumentException
- Set the time-out period. This is the time a read() will wait
for a byte of data to read. If the time is exceeded a the read will
throw an exception.
- Parameters:
- 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.
- Throws: IllegalArgumentException
- if timeout < 0.
- See Also:
- read
read
public int read() throws IOException, InterruptedIOException
- Reads the next byte of data from the input stream. The value
byte is returned as an
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.
- Returns:
- the next byte of data, or
-1
if the end of the
stream is reached.
- Throws: IOException
- if an I/O error occurs.
- Throws: InterruptedIOException
- if the time-out period is
exceeded in waiting for a byte to read or the thread is interrupted..
- Overrides:
- read in class FilterInputStream
read
public int read(byte inputBuffer[],
int offset,
int length) throws IOException, InterruptedIOException
- Reads up to
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.
- Parameters:
- inputBuffer - the buffer into which the data is read.
- offset - the start offset of the data.
- length - the maximum number of bytes read.
- Returns:
- the total number of bytes read into the buffer, or
-1
if there is no more data because the end of
the stream has been reached.
- Throws: IOException
- if an I/O error occurs.
- Throws: InterruptedIOException
- if the time-out period is
exceeded in waiting for a byte to read or the thread is interrupted..
- Overrides:
- read in class FilterInputStream
- See Also:
- read
All Packages Class Hierarchy This Package Previous Next Index