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
ConstructorsConstructorDescriptionIdleTimeoutInputStream(InputStream in, long timeout) Convenience constructor.IdleTimeoutInputStream(InputStream in, String threadName, long timeout) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionintvoidCheck the status of the underlyingInputStream.voidclose()voidHandle end-of-file detected on the underlying input.voidHandle an exception detected on the underlying input.voidhandleInput(byte[] buf, int off, int len) Handle new data read from the underlying input.intread()intread(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- iftimeoutis 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- iftimeoutis negative
-
-
Method Details
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream
-
available
- Overrides:
availablein classInputStream- Throws:
IOException
-
checkConnection
Check the status of the underlyingInputStream.This method guarantees that if the underlying
InputStreamhas been closed, reached EOF, or thrown an exception, then this method will throw an immediate exception.- Throws:
EOFException- if the underlyingInputStreamhas reached EOFIOException- if the underlyingInputStreamthrew anIOExceptionIOException- if the underlyingInputStreamhas been closed
-
handleInput
public void handleInput(byte[] buf, int off, int len) Description copied from interface:AsyncInputStream.ListenerHandle new data read from the underlying input. This method must not write to buffer bytes outside of the defined region.- Specified by:
handleInputin 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.ListenerHandle end-of-file detected on the underlying input. No further events will be delivered.Typically the assocaited
AsyncInputStreamwill be closed in this callback.- Specified by:
handleEOFin interfaceAsyncInputStream.Listener
-
handleException
Description copied from interface:AsyncInputStream.ListenerHandle an exception detected on the underlying input. No further events will be delivered.Typically the assocaited
AsyncInputStreamwill be closed in this callback.- Specified by:
handleExceptionin interfaceAsyncInputStream.Listener- Parameters:
e- the exception received (usuallyIOExceptionbut could also be any otherRuntimeException)
-