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 class
Dummy implementation of aTimeoutInputStream
directly delegating to the givenInputStream
to test whether theTimeoutInputStream
has 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 aTimeoutInputStream
decorating anInputStream
with additional timeout functionality.TimeoutInputStream
(InputStream aInputStream, long aTimeoutMillis) Constructs aTimeoutInputStream
decorating anInputStream
with additional timeout functionality.TimeoutInputStream
(InputStream aInputStream, long aTimeoutMillis, Object aMonitor) Constructs aTimeoutInputStream
decorating anInputStream
with additional timeout functionality.TimeoutInputStream
(InputStream aInputStream, Long aTimeoutMillis) Constructs aTimeoutInputStream
decorating anInputStream
with additional timeout functionality.TimeoutInputStream
(InputStream aInputStream, Object aMonitor) Constructs aTimeoutInputStream
decorating anInputStream
with additional timeout functionality. -
Method Summary
Modifier and TypeMethodDescriptionint
void
close()
long
void
mark
(int readlimit) boolean
int
read()
int
read
(byte[] b) int
read
(byte[] b, int off, int len) int
read
(byte[] b, int off, int len, long aTimeoutMillis) Enriches theread(byte[], int, int)
method with a timeout.int
read
(byte[] b, long aTimeoutMillis) Enriches theread(byte[])
method with a timeout.int
read
(long aTimeoutMillis) Enriches theread()
method with a timeout.int
readNBytes
(byte[] b, int off, int len) int
readNBytes
(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.void
reset()
long
skip
(long n) Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, skipNBytes, transferTo
-
Constructor Details
-
TimeoutInputStream
Constructs aTimeoutInputStream
decorating anInputStream
with additional timeout functionality.- Parameters:
aInputStream
- TheInputStream
to 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 aTimeoutInputStream
decorating anInputStream
with additional timeout functionality.- Parameters:
aInputStream
- TheInputStream
to 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 aTimeoutInputStream
decorating anInputStream
with additional timeout functionality.- Parameters:
aInputStream
- TheInputStream
to 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 aTimeoutInputStream
decorating anInputStream
with additional timeout functionality.- Parameters:
aInputStream
- TheInputStream
to be decorated.
-
TimeoutInputStream
Constructs aTimeoutInputStream
decorating anInputStream
with additional timeout functionality.- Parameters:
aInputStream
- TheInputStream
to 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:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
readNBytes
- Overrides:
readNBytes
in classInputStream
- Throws:
IOException
-
readNBytes
- Overrides:
readNBytes
in classInputStream
- Throws:
IOException
-
available
- Overrides:
available
in classInputStream
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
mark
public void mark(int readlimit) - Overrides:
mark
in classInputStream
-
markSupported
public boolean markSupported()- Overrides:
markSupported
in classInputStream
-
reset
- Overrides:
reset
in classInputStream
- Throws:
IOException
-
skip
- Overrides:
skip
in 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 inb
at 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
- ifb
isnull
IndexOutOfBoundsException
- Ifoff
is negative,len
is negative, orlen
is 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:
getReadTimeoutMillis
in interfaceorg.refcodes.mixin.ReadTimeoutMillisAccessor
-