Class IdleTimeoutInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
,AsyncInputStream.Listener
InputStream
and enforces a maximum time limit on how long any read()
operation may block. If the time limit is exceeded, an IdleTimeoutException
is thrown.
As a side effect of its design, this class may also be used to artificially inject data, EOF, or exceptions into the
InputStream
, using the AsyncInputStream.Listener
interface methods.
This class may also be used to allow testing the real-time state of an InputStream
without actually reading
from it, via checkConnection()
, which guarantees an immediate exception will be thrown if the underlying
InputStream
has been closed, reached EOF, or itself thrown an exception.
All methods in this class are thread safe.
-
Constructor Summary
ConstructorDescriptionIdleTimeoutInputStream
(InputStream in, long timeout) Convenience constructor.IdleTimeoutInputStream
(InputStream in, String threadName, long timeout) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionint
void
Check the status of the underlyingInputStream
.void
close()
void
Handle end-of-file detected on the underlying input.void
Handle an exception detected on the underlying input.void
handleInput
(byte[] buf, int off, int len) Handle new data read from the underlying input.int
read()
int
read
(byte[] buf, int off, int len) Methods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
Constructor Details
-
IdleTimeoutInputStream
Constructor.- Parameters:
in
- input sourcethreadName
- name for the reader thread, or null for defaulttimeout
- maximum input idle time in milliseconds, or zero for infinity- Throws:
IllegalArgumentException
- iftimeout
is negative
-
IdleTimeoutInputStream
Convenience constructor. Equivalent to:IdleTimeoutInputStream(in, null, timeout);
- Parameters:
in
- input sourcetimeout
- maximum input idle time in milliseconds, or zero for infinity- Throws:
IllegalArgumentException
- iftimeout
is negative
-
-
Method Details
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
-
available
- Overrides:
available
in classInputStream
- Throws:
IOException
-
checkConnection
Check the status of the underlyingInputStream
.This method guarantees that if the underlying
InputStream
has been closed, reached EOF, or thrown an exception, then this method will throw an immediate exception.- Throws:
EOFException
- if the underlyingInputStream
has reached EOFIOException
- if the underlyingInputStream
threw anIOException
IOException
- if the underlyingInputStream
has been closed
-
handleInput
public void handleInput(byte[] buf, int off, int len) Description copied from interface:AsyncInputStream.Listener
Handle new data read from the underlying input. This method must not write to buffer bytes outside of the defined region.- Specified by:
handleInput
in interfaceAsyncInputStream.Listener
- Parameters:
buf
- data bufferoff
- starting offset of data in bufferlen
- number of bytes of data
-
handleEOF
public void handleEOF()Description copied from interface:AsyncInputStream.Listener
Handle end-of-file detected on the underlying input. No further events will be delivered.Typically the assocaited
AsyncInputStream
will be closed in this callback.- Specified by:
handleEOF
in interfaceAsyncInputStream.Listener
-
handleException
Description copied from interface:AsyncInputStream.Listener
Handle an exception detected on the underlying input. No further events will be delivered.Typically the assocaited
AsyncInputStream
will be closed in this callback.- Specified by:
handleException
in interfaceAsyncInputStream.Listener
- Parameters:
e
- the exception received (usuallyIOException
but could also be any otherRuntimeException
)
-