Class TimeoutReader

  • All Implemented Interfaces:
    Closeable, AutoCloseable, Readable

    public class TimeoutReader
    extends Reader
    Reads text from a parent reader with the given timeout.

    Each read request causes a TimeoutReader request an underlying reader to read with the specified timeout. IOException will be thrown, if timeout is reached. Clients may wrap a TimeoutReader around any input stream reader whose read() operations may hang out, such as Process.getInputStream(). Example of usage:

     // reads process input with 10 seconds timeout
     try (TimeoutReader reader = new TimeoutReader(
         new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8), 10 * 1000))
     {
         ...
     }
     
    • Constructor Detail

      • TimeoutReader

        public TimeoutReader​(Reader input,
                             int timeout)
        Creates a TimeoutReader that has a given timeout in milliseconds.
        Parameters:
        input - the parent reader to wrap, cannot be null
        timeout - the maximum time to wait in milliseconds
      • TimeoutReader

        public TimeoutReader​(Reader input,
                             int timeout,
                             TimeUnit timeUnit)
        Creates a TimeoutReader that has a given timeout.
        Parameters:
        input - the parent reader to wrap, cannot be null
        timeout - the maximum time to wait
        timeUnit - the time unit of the timeout argument