Class TimeoutInputStream

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 t9o the number of bytes to be read.
  • Field Details

    • _inputStream

      protected InputStream _inputStream
    • _readTimeoutMillis

      protected long _readTimeoutMillis
    • _isClosed

      protected boolean _isClosed
  • Constructor Details

    • TimeoutInputStream

      public TimeoutInputStream(InputStream aInputStream, long aTimeoutMillis)
      Constructs a TimeoutInputStream decorating an InputStream with additional timeout functionality.
      Parameters:
      aInputStream - The InputStream 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

      public TimeoutInputStream(InputStream aInputStream, ExecutorService aExecutorService)
      Constructs a TimeoutInputStream decorating an InputStream with additional timeout functionality.
      Parameters:
      aInputStream - The InputStream to be decorated.
      aExecutorService - The ExecutorService to be used when creating threads.
    • TimeoutInputStream

      public TimeoutInputStream(InputStream aInputStream)
      Constructs a TimeoutInputStream decorating an InputStream with additional timeout functionality.
      Parameters:
      aInputStream - The InputStream to be decorated.
    • TimeoutInputStream

      public TimeoutInputStream(InputStream aInputStream, long aTimeoutMillis, ExecutorService aExecutorService)
      Constructs a TimeoutInputStream decorating an InputStream with additional timeout functionality.
      Parameters:
      aInputStream - The InputStream 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 - The ExecutorService to be used when creating threads.
  • Method Details

    • read

      public int read() throws IOException
      Caution: The timeout (as of getReadTimeoutMillis()) is being applied! A timeout value of -1 when calling read(long) disables the timeout!
      Specified by:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Caution: The timeout (as of getReadTimeoutMillis()) is being applied! A timeout value of -1 when calling read(byte[], int, int, long) disables the timeout!
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b) throws IOException
      Caution: The timeout (as of getReadTimeoutMillis()) is being applied! A timeout value of -1 when calling read(byte[], long) disables the timeout!
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • readNBytes

      public int readNBytes(byte[] b, int off, int len) throws IOException
      Caution: The timeout (as of getReadTimeoutMillis()) is being applied! A timeout value of -1 when calling readNBytes(byte[], int, int, long) disables the timeout!
      Overrides:
      readNBytes in class InputStream
      Throws:
      IOException
    • readNBytes

      public byte[] readNBytes(int len) throws IOException
      Caution: The timeout (as of getReadTimeoutMillis()) is being applied! A timeout value of -1 when calling readNBytes(int, long) disables the timeout!
      Overrides:
      readNBytes in class InputStream
      Throws:
      IOException
    • available

      public int available() throws IOException
      Overrides:
      available in class InputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException
    • mark

      public void mark(int readlimit)
      Overrides:
      mark in class InputStream
    • markSupported

      public boolean markSupported()
      Overrides:
      markSupported in class InputStream
    • reset

      public void reset() throws IOException
      Overrides:
      reset in class InputStream
      Throws:
      IOException
    • read

      public int read(long aTimeoutMillis) throws IOException
      Enriches the read() 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

      public int read(byte[] b, int off, int len, long aTimeoutMillis) throws IOException
      Enriches the read(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

      public int read(byte[] b, long aTimeoutMillis) throws IOException
      Enriches the read(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 a read(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

      public int readNBytes(byte[] b, int off, int len, long aTimeoutMillis) throws IOException
      Enriches the readNBytes(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.
      Returns:
      the actual number of bytes read into the buffer
      Throws:
      IOException - if an I/O error occurs
      NullPointerException - if b is null
      IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off
    • readNBytes

      public byte[] readNBytes(int len, long aTimeoutMillis) throws IOException
      Enriches the readNBytes(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 read
      aTimeoutMillis - 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

      public long skip(long n, long aTimeoutMillis) throws IOException
      Enriches the InputStream.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

      public void skipNBytes(long n, long aTimeoutMillis) throws IOException
      Enriches the InputStream.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

      public long transferTo(OutputStream out, long aTimeoutMillis) throws IOException
      Enriches the InputStream.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-null
      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 number of bytes transferred
      Throws:
      IOException - thrown in case of according I/O related problems or an expired timeout.
    • readAllBytes

      public byte[] readAllBytes(long aTimeoutMillis) throws IOException
      Enriches the InputStream.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 interface org.refcodes.mixin.ReadTimeoutMillisAccessor