Class TimeoutInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class TimeoutInputStream
    extends java.io.FilterInputStream
    InputStream with a configurable timeout.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private InterruptTimer myTimer  
      private int timeout  
      • Fields inherited from class java.io.FilterInputStream

        in
    • Constructor Summary

      Constructors 
      Constructor Description
      TimeoutInputStream​(java.io.InputStream src, InterruptTimer timer)
      Wrap an input stream with a timeout on all read operations.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void beginRead()  
      private void endRead()  
      int getTimeout()
      Get number of milliseconds before aborting a read.
      int read()
      int read​(byte[] buf)
      int read​(byte[] buf, int off, int cnt)
      private java.io.InterruptedIOException readTimedOut​(java.io.InterruptedIOException e)  
      void setTimeout​(int millis)
      Set number of milliseconds before aborting a read.
      long skip​(long cnt)
      • Methods inherited from class java.io.FilterInputStream

        available, close, mark, markSupported, reset
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • timeout

        private int timeout
    • Constructor Detail

      • TimeoutInputStream

        public TimeoutInputStream​(java.io.InputStream src,
                                  InterruptTimer timer)
        Wrap an input stream with a timeout on all read operations.
        Parameters:
        src - base input stream (to read from). The stream must be interruptible (most socket streams are).
        timer - timer to manage the timeouts during reads.
    • Method Detail

      • getTimeout

        public int getTimeout()
        Get number of milliseconds before aborting a read.
        Returns:
        number of milliseconds before aborting a read.
      • setTimeout

        public void setTimeout​(int millis)
        Set number of milliseconds before aborting a read.
        Parameters:
        millis - number of milliseconds before aborting a read. Must be > 0.
      • read

        public int read()
                 throws java.io.IOException
        Overrides:
        read in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] buf)
                 throws java.io.IOException
        Overrides:
        read in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] buf,
                        int off,
                        int cnt)
                 throws java.io.IOException
        Overrides:
        read in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • skip

        public long skip​(long cnt)
                  throws java.io.IOException
        Overrides:
        skip in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • beginRead

        private void beginRead()
      • endRead

        private void endRead()
      • readTimedOut

        private java.io.InterruptedIOException readTimedOut​(java.io.InterruptedIOException e)