Class TimeoutInputStream
- All Implemented Interfaces:
Closeable, AutoCloseable, ReadTimeoutMillisAccessor
- Direct Known Subclasses:
AvailableInputStream
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 ReadTimeoutMillisAccessor
ReadTimeoutMillisAccessor.ReadTimeoutMillisBuilder<B>, ReadTimeoutMillisAccessor.ReadTimeoutMillisMutator, ReadTimeoutMillisAccessor.ReadTimeoutMillisProperty -
Field Summary
Fields -
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, ExecutorService aExecutorService) Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.TimeoutInputStream(InputStream aInputStream, ExecutorService aExecutorService) 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.byte[]readAllBytes(long aTimeoutMillis) Enriches theInputStream.readAllBytes()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, long aTimeoutMillis) Enriches theInputStream.skip(long)method with a timeout.voidskipNBytes(long n, long aTimeoutMillis) Enriches theInputStream.skipNBytes(long)method with a timeout.longtransferTo(OutputStream out, long aTimeoutMillis) Enriches theInputStream.transferTo(OutputStream)method with a timeout.Methods inherited from class InputStream
nullInputStream, readAllBytes, skip, skipNBytes, transferTo
-
Field Details
-
_inputStream
-
_readTimeoutMillis
protected long _readTimeoutMillis -
_isClosed
protected boolean _isClosed
-
-
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.aExecutorService- TheExecutorServiceto be used when creating threads.
-
TimeoutInputStream
Constructs aTimeoutInputStreamdecorating anInputStreamwith additional timeout functionality.- Parameters:
aInputStream- TheInputStreamto be decorated.
-
TimeoutInputStream
public TimeoutInputStream(InputStream aInputStream, long aTimeoutMillis, ExecutorService aExecutorService) 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).aExecutorService- TheExecutorServiceto be used when creating threads.
-
-
Method Details
-
read
Caution: The timeout (as of
getReadTimeoutMillis()) is being applied! A timeout value of-1when callingread(long)disables the timeout!- Specified by:
readin classInputStream- Throws:
IOException
-
read
Caution: The timeout (as of
getReadTimeoutMillis()) is being applied! A timeout value of-1when callingread(byte[], int, int, long)disables the timeout!- Overrides:
readin classInputStream- Throws:
IOException
-
read
Caution: The timeout (as of
getReadTimeoutMillis()) is being applied! A timeout value of-1when callingread(byte[], long)disables the timeout!- Overrides:
readin classInputStream- Throws:
IOException
-
readNBytes
Caution: The timeout (as of
getReadTimeoutMillis()) is being applied! A timeout value of-1when callingreadNBytes(byte[], int, int, long)disables the timeout!- Overrides:
readNBytesin classInputStream- Throws:
IOException
-
readNBytes
Caution: The timeout (as of
getReadTimeoutMillis()) is being applied! A timeout value of-1when callingreadNBytes(int, long)disables the timeout!- 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
-
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 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.- 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:
getReadTimeoutMillisin interfaceReadTimeoutMillisAccessor
-