- 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
TheTimeoutInputStreamdecorates anInputStreamwith 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 decoratedInputStreamreturns realistic values when callingInputStream.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
All Methods Instance Methods Concrete Methods 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, transferTo
-
-
-
-
Constructor Detail
-
TimeoutInputStream
public TimeoutInputStream(InputStream aInputStream, long aTimeoutInMs)
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
public TimeoutInputStream(InputStream aInputStream, Object aMonitor)
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
public TimeoutInputStream(InputStream aInputStream)
Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.- Parameters:
aInputStream- TheInputStreamto be decorated.
-
TimeoutInputStream
public TimeoutInputStream(InputStream aInputStream, long aTimeoutInMs, Object aMonitor)
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 Detail
-
read
public int read() throws IOException- Specified by:
readin classInputStream- Throws:
IOException
-
read
public int read(byte[] b, int off, int len) throws IOException- Overrides:
readin classInputStream- Throws:
IOException
-
read
public int read(byte[] b) throws IOException- Overrides:
readin classInputStream- Throws:
IOException
-
readNBytes
public int readNBytes(byte[] b, int off, int len) throws IOException- Overrides:
readNBytesin classInputStream- Throws:
IOException
-
available
public int available() throws IOException- Overrides:
availablein classInputStream- Throws:
IOException
-
close
public void close() throws IOException- 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
public void reset() throws IOException- Overrides:
resetin classInputStream- Throws:
IOException
-
skip
public long skip(long n) throws IOException- Overrides:
skipin classInputStream- Throws:
IOException
-
read
public int read(long aTimeoutInMs) throws IOExceptionEnriches 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
public int read(byte[] b, int off, int len, long aTimeoutInMs) throws IOExceptionEnriches 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
public int read(byte[] b, long aTimeoutInMs) throws IOExceptionEnriches 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
public int readNBytes(byte[] b, int off, int len, long aTimeoutInMs) throws IOExceptionEnriches 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 readaTimeoutInMs- 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
-
getReadTimeoutInMs
public long getReadTimeoutInMs()
- Specified by:
getReadTimeoutInMsin interfaceorg.refcodes.mixin.ReadTimeoutInMsAccessor
-
-