java.lang.Object
java.io.InputStream
org.refcodes.io.TimeoutInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
,org.refcodes.mixin.ReadTimeoutMillisAccessor
- Direct Known Subclasses:
AvailableInputStream
public class TimeoutInputStream
extends InputStream
implements org.refcodes.mixin.ReadTimeoutMillisAccessor
The
TimeoutInputStream
decorates an InputStream
with time-out
functionality using CompletableFuture
functionality. This decorator
works despite the decorated InputStream
returning realistic values
when calling InputStream.available()
. If this can be guaranteed, you
may use the AvailableInputStream
! The benefit of the
AvailableInputStream
over the TimeoutInputStream
is that the
AvailableInputStream
does not use any additional threads for
asynchronous operation! Additional thread is skipped in case the
TimeoutInputStream
timeout is -1 or the available (as of
available()
) number of bytes is greater or equal to the number of
bytes to be read.-
Nested Class Summary
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
-
Field Summary
-
Constructor Summary
ConstructorDescriptionTimeoutInputStream
(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, ExecutorService aExecutorService) Constructs aTimeoutInputStream
decorating anInputStream
with additional timeout functionality.TimeoutInputStream
(InputStream aInputStream, ExecutorService aExecutorService) Constructs aTimeoutInputStream
decorating anInputStream
with additional timeout functionality. -
Method Summary
Modifier and TypeMethodDescriptionint
void
close()
long
void
mark
(int readlimit) boolean
int
read()
Caution: The timeout (as ofgetReadTimeoutMillis()
) is being applied!int
read
(byte[] b) Caution: The timeout (as ofgetReadTimeoutMillis()
) is being applied!int
read
(byte[] b, int off, int len) Caution: The timeout (as ofgetReadTimeoutMillis()
) is being applied!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.byte[]
readAllBytes
(long aTimeoutMillis) Enriches theInputStream.readAllBytes()
method with a timeout.int
readNBytes
(byte[] b, int off, int len) Caution: The timeout (as ofgetReadTimeoutMillis()
) is being applied!int
readNBytes
(byte[] b, int off, int len, long aTimeoutMillis) Enriches thereadNBytes(byte[], int, int)
method with a timeout.byte[]
readNBytes
(int len) Caution: The timeout (as ofgetReadTimeoutMillis()
) is being applied!byte[]
readNBytes
(int len, long aTimeoutMillis) Enriches thereadNBytes(int)
method with a timeout.void
reset()
long
skip
(long n, long aTimeoutMillis) Enriches theInputStream.skip(long)
method with a timeout.void
skipNBytes
(long n, long aTimeoutMillis) Enriches theInputStream.skipNBytes(long)
method with a timeout.long
transferTo
(OutputStream out, long aTimeoutMillis) Enriches theInputStream.transferTo(OutputStream)
method with a timeout.Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, skip, skipNBytes, transferTo
-
Field Details
-
_inputStream
-
_readTimeoutMillis
protected long _readTimeoutMillis -
_isClosed
protected boolean _isClosed
-
-
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.aExecutorService
- TheExecutorService
to be used when creating threads.
-
TimeoutInputStream
Constructs aTimeoutInputStream
decorating anInputStream
with additional timeout functionality.- Parameters:
aInputStream
- TheInputStream
to be decorated.
-
TimeoutInputStream
public TimeoutInputStream(InputStream aInputStream, long aTimeoutMillis, ExecutorService aExecutorService) 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).aExecutorService
- TheExecutorService
to be used when creating threads.
-
-
Method Details
-
read
Caution: The timeout (as ofgetReadTimeoutMillis()
) is being applied! A timeout value of-1
when callingread(long)
disables the timeout!- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
Caution: The timeout (as ofgetReadTimeoutMillis()
) is being applied! A timeout value of-1
when callingread(byte[], int, int, long)
disables the timeout!- Overrides:
read
in classInputStream
- Throws:
IOException
-
read
Caution: The timeout (as ofgetReadTimeoutMillis()
) is being applied! A timeout value of-1
when callingread(byte[], long)
disables the timeout!- Overrides:
read
in classInputStream
- Throws:
IOException
-
readNBytes
Caution: The timeout (as ofgetReadTimeoutMillis()
) is being applied! A timeout value of-1
when callingreadNBytes(byte[], int, int, long)
disables the timeout!- Overrides:
readNBytes
in classInputStream
- Throws:
IOException
-
readNBytes
Caution: The timeout (as ofgetReadTimeoutMillis()
) is being applied! A timeout value of-1
when callingreadNBytes(int, long)
disables the timeout!- 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
-
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 next byte of data, or -1 if the end of the stream is reached.
- 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 total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
- 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 total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
- 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.- Parameters:
len
- the maximum number of bytes to readaTimeoutMillis
- The timeout in milliseconds.- Returns:
- The byte array containing the bytes read from this input stream
- Throws:
IOException
- Signals that an I/O exception has occurred.
-
skip
Enriches theInputStream.skip(long)
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:
n
- the number of bytes to be skipped.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 actual number of bytes skipped which might be zero.
- Throws:
IOException
- thrown in case of according I/O related problems or an expired timeout.
-
skipNBytes
Enriches theInputStream.skipNBytes(long)
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:
n
- the number of bytes to be skipped.aTimeoutMillis
- The timeout in milliseconds to wait for the next byte available.With a value of -1 timeout handling is disabled (blocking mode).- Throws:
IOException
- thrown in case of according I/O related problems or an expired timeout.
-
transferTo
Enriches theInputStream.transferTo(OutputStream)
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:
out
- the output stream, non-nullaTimeoutMillis
- The timeout in milliseconds to wait for the next byte available.With a value of -1 timeout handling is disabled (blocking mode).- Returns:
- the number of bytes transferred
- Throws:
IOException
- thrown in case of according I/O related problems or an expired timeout.
-
readAllBytes
Enriches theInputStream.readAllBytes()
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:
- a byte array containing the bytes read from this input stream
- Throws:
IOException
- thrown in case of according I/O related problems or an expired timeout.
-
getReadTimeoutMillis
public long getReadTimeoutMillis()- Specified by:
getReadTimeoutMillis
in interfaceorg.refcodes.mixin.ReadTimeoutMillisAccessor
-