java.lang.Object
java.io.InputStream
org.refcodes.io.TimeoutInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable,org.refcodes.mixin.ReadTimeoutInMsAccessor
- Direct Known Subclasses:
TimeoutInputStream.DummyTimeoutInputStream
public class TimeoutInputStream extends InputStream implements org.refcodes.mixin.ReadTimeoutInMsAccessor
The
TimeoutInputStream decorates an InputStream with time-out
functionality. To avoid performance issues due to exhausting synchronization,
this class is not thread safe (take care of synchronizing yourself when
accessing from multiple threads). This decorator works only when the
decorated InputStream returns realistic values when calling
InputStream.available().-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTimeoutInputStream.DummyTimeoutInputStreamDummy implementation of aTimeoutInputStreamdirectly delegating to the givenInputStreamto test whether theTimeoutInputStreamhas any latencies compared to theTimeoutInputStream.DummyTimeoutInputStream.Nested classes/interfaces inherited from interface org.refcodes.mixin.ReadTimeoutInMsAccessor
org.refcodes.mixin.ReadTimeoutInMsAccessor.ReadTimeoutInMsBuilder<B extends org.refcodes.mixin.ReadTimeoutInMsAccessor.ReadTimeoutInMsBuilder<B>>, org.refcodes.mixin.ReadTimeoutInMsAccessor.ReadTimeoutInMsMutator, org.refcodes.mixin.ReadTimeoutInMsAccessor.ReadTimeoutInMsProperty -
Constructor Summary
Constructors Constructor Description TimeoutInputStream(InputStream aInputStream)Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.TimeoutInputStream(InputStream aInputStream, long aTimeoutInMs)Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.TimeoutInputStream(InputStream aInputStream, long aTimeoutInMs, Object aMonitor)Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.TimeoutInputStream(InputStream aInputStream, Object aMonitor)Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality. -
Method Summary
Modifier and Type Method Description intavailable()voidclose()longgetReadTimeoutInMs()voidmark(int readlimit)booleanmarkSupported()intread()intread(byte[] b)intread(byte[] b, int off, int len)intread(byte[] b, int off, int len, long aTimeoutInMs)Enriches theread(byte[], int, int)method with a timeout.intread(byte[] b, long aTimeoutInMs)Enriches theread(byte[])method with a timeout.intread(long aTimeoutInMs)Enriches theread()method with a timeout.intreadNBytes(byte[] b, int off, int len)intreadNBytes(byte[] b, int off, int len, long aTimeoutInMs)Enriches thereadNBytes(byte[], int, int)method with a timeout.voidreset()longskip(long n)Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, skipNBytes, transferTo
-
Constructor Details
-
TimeoutInputStream
Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.- Parameters:
aInputStream- TheInputStreamto be decorated.aTimeoutInMs- The default timeout for read operations not explicitly called with a timeout argument. With a value of -1 timeout handling is disabled (blocking mode).
-
TimeoutInputStream
Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.- Parameters:
aInputStream- TheInputStreamto be decorated.aMonitor- The monitor to use when waiting the poll loop time. This is useful required available data can be read before the poll loop time expires. E.g. an underlying system might callObject.notifyAll()on the monitor as soon as it received new data.
-
TimeoutInputStream
Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.- Parameters:
aInputStream- TheInputStreamto be decorated.
-
TimeoutInputStream
Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.- Parameters:
aInputStream- TheInputStreamto be decorated.aTimeoutInMs- The default timeout for read operations not explicitly called with a timeout argument. With a value of -1 timeout handling is disabled (blocking mode).aMonitor- The monitor to use when waiting the poll loop time. This is useful required available data can be read before the poll loop time expires. E.g. an underlying system might callObject.notifyAll()on the monitor as soon as it received new data.
-
-
Method Details
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
readNBytes
- Overrides:
readNBytesin classInputStream- Throws:
IOException
-
available
- Overrides:
availablein classInputStream- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
mark
public void mark(int readlimit)- Overrides:
markin classInputStream
-
markSupported
public boolean markSupported()- Overrides:
markSupportedin classInputStream
-
reset
- Overrides:
resetin classInputStream- Throws:
IOException
-
skip
- Overrides:
skipin classInputStream- Throws:
IOException
-
read
Enriches theread()method with a timeout. This methods blocks till the result is available and can be returned or the timeout is reached. A timeout of -1 disables the timeout.- Parameters:
aTimeoutInMs- The timeout in milliseconds to wait for the next byte available.With a value of -1 timeout handling is disabled (blocking mode).- Returns:
- the int
- Throws:
IOException- thrown in case of according I/O related problems or an expired timeout.
-
read
Enriches theread(byte[], int, int)method with a timeout. This methods blocks till the result is available and can be returned or the timeout is reached. A timeout of -1 disables the timeout.- Parameters:
b- The byte array into which the data is read.off- The start offset in b at which the data is written.len- The maximum number of bytes to read.aTimeoutInMs- The timeout in milliseconds to wait for the next byte available.With a value of -1 timeout handling is disabled (blocking mode).- Returns:
- the int
- Throws:
IOException- thrown in case of according I/O related problems or an expired timeout.
-
read
Enriches theread(byte[])method with a timeout. This methods blocks till the result is available and can be returned or the timeout is reached. A timeout of -1 disables the timeout. Calling this method has the same effect aread(b, 0, b.length), so we try to read as many bytes as the buffer's length is.- Parameters:
b- The byte array into which the data is read.aTimeoutInMs- The timeout in milliseconds to wait for the next byte available.With a value of -1 timeout handling is disabled (blocking mode).- Returns:
- the int
- Throws:
IOException- thrown in case of according I/O related problems or an expired timeout.
-
readNBytes
Enriches thereadNBytes(byte[], int, int)method with a timeout. This methods blocks till the result is available and can be returned or the timeout is reached. A timeout of -1 disables the timeout.- Parameters:
b- the boff- the offlen- the lenaTimeoutInMs- the timeout in ms- Returns:
- the read timeout in ms
- Throws:
IOException- Signals that an I/O exception has occurred.
-
getReadTimeoutInMs
public long getReadTimeoutInMs()- Specified by:
getReadTimeoutInMsin interfaceorg.refcodes.mixin.ReadTimeoutInMsAccessor
-