java.lang.Object
java.io.InputStream
org.refcodes.io.TimeoutInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable,org.refcodes.mixin.ReadTimeoutMillisAccessor
- Direct Known Subclasses:
TimeoutInputStream.DummyTimeoutInputStream
public class TimeoutInputStream
extends InputStream
implements org.refcodes.mixin.ReadTimeoutMillisAccessor
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 ClassesModifier and TypeClassDescriptionstatic classDummy 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.ReadTimeoutMillisAccessor
org.refcodes.mixin.ReadTimeoutMillisAccessor.ReadTimeoutMillisBuilder<B extends org.refcodes.mixin.ReadTimeoutMillisAccessor.ReadTimeoutMillisBuilder<B>>, org.refcodes.mixin.ReadTimeoutMillisAccessor.ReadTimeoutMillisMutator, org.refcodes.mixin.ReadTimeoutMillisAccessor.ReadTimeoutMillisProperty -
Constructor Summary
ConstructorsConstructorDescriptionTimeoutInputStream(InputStream aInputStream) Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.TimeoutInputStream(InputStream aInputStream, long aTimeoutMillis) Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.TimeoutInputStream(InputStream aInputStream, long aTimeoutMillis, Object aMonitor) Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.TimeoutInputStream(InputStream aInputStream, Long aTimeoutMillis) Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.TimeoutInputStream(InputStream aInputStream, Object aMonitor) Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality. -
Method Summary
Modifier and TypeMethodDescriptionintvoidclose()longvoidmark(int readlimit) booleanintread()intread(byte[] b) intread(byte[] b, int off, int len) intread(byte[] b, int off, int len, long aTimeoutMillis) Enriches theread(byte[], int, int)method with a timeout.intread(byte[] b, long aTimeoutMillis) Enriches theread(byte[])method with a timeout.intread(long aTimeoutMillis) Enriches theread()method with a timeout.intreadNBytes(byte[] b, int off, int len) intreadNBytes(byte[] b, int off, int len, long aTimeoutMillis) Enriches thereadNBytes(byte[], int, int)method with a timeout.byte[]readNBytes(int len) byte[]readNBytes(int len, long aTimeoutMillis) Enriches thereadNBytes(int)method with a timeout.voidreset()longskip(long n) Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, skipNBytes, transferTo
-
Constructor Details
-
TimeoutInputStream
Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.- Parameters:
aInputStream- TheInputStreamto be decorated.aTimeoutMillis- 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.aTimeoutMillis- 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.aTimeoutMillis- 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
-
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:
aTimeoutMillis- 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.aTimeoutMillis- 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.aTimeoutMillis- 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 byte array into which the data is readoff- the start offset inbat which the data is writtenlen- the maximum number of bytes to readaTimeoutMillis- The timeout in milliseconds.- Returns:
- the actual number of bytes read into the buffer
- Throws:
IOException- if an I/O error occursNullPointerException- ifbisnullIndexOutOfBoundsException- Ifoffis negative,lenis negative, orlenis greater thanb.length - off
-
readNBytes
Enriches thereadNBytes(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.- Returns:
- the read timeout in ms
- Throws:
IOException
-
getReadTimeoutMillis
public long getReadTimeoutMillis()- Specified by:
getReadTimeoutMillisin interfaceorg.refcodes.mixin.ReadTimeoutMillisAccessor
-