sdsu.io
Class NonBlockingInputStream

java.lang.Object
  |
  +--java.io.InputStream
        |
        +--java.io.FilterInputStream
              |
              +--sdsu.io.NonBlockingInputStream

public class NonBlockingInputStream
extends java.io.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)

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

NonBlockingInputStream

public 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

public NonBlockingInputStream(java.io.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:
java.lang.IllegalArgumentException - if pollDelay < 0 or timeout < pollDelay.
Method Detail

setPollDelay

public void setPollDelay(int delay)
                  throws java.lang.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:
java.lang.IllegalArgumentException - if duration < 0.

setTimeout

public void setTimeout(long timeout)
                throws java.lang.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:
java.lang.IllegalArgumentException - if timeout < 0.
See Also:
read()

read

public int read()
         throws java.io.IOException,
                java.io.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:
java.io.IOException - if an I/O error occurs.
java.io.InterruptedIOException - if the time-out period is exceeded in waiting for a byte to read or the thread is interrupted..
Overrides:
read in class java.io.FilterInputStream

read

public int read(byte[] inputBuffer,
                int offset,
                int length)
         throws java.io.IOException,
                java.io.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:
java.io.IOException - if an I/O error occurs.
java.io.InterruptedIOException - if the time-out period is exceeded in waiting for a byte to read or the thread is interrupted..
Overrides:
read in class java.io.FilterInputStream
See Also:
InputStream.read()