Class TimeoutInputStream

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().
  • 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, 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, Object aMonitor)
      Constructs a TimeoutInputStream decorating an InputStream with additional timeout functionality.
      Parameters:
      aInputStream - The InputStream 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 call Object.notifyAll() on the monitor as soon as it received new data.
    • 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, Object aMonitor)
      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).
      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 call Object.notifyAll() on the monitor as soon as it received new data.
  • Method Details

    • read

      public int read() throws IOException
      Specified by:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • readNBytes

      public int readNBytes(byte[] b, int off, int len) throws IOException
      Overrides:
      readNBytes in class InputStream
      Throws:
      IOException
    • readNBytes

      public byte[] readNBytes(int len) throws IOException
      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
    • skip

      public long skip(long n) throws IOException
      Overrides:
      skip 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 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 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 int
      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 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 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.
      Returns:
      the read timeout in ms
      Throws:
      IOException
    • getReadTimeoutMillis

      public long getReadTimeoutMillis()
      Specified by:
      getReadTimeoutMillis in interface org.refcodes.mixin.ReadTimeoutMillisAccessor