Class ThrottledInputStream

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

    public final class ThrottledInputStream
    extends CountingInputStream
    Provides bandwidth throttling on an InputStream as a filter input stream. The throttling examines the number of bytes read from the underlying InputStream, and sleeps for a time interval if the byte-transfer is found to exceed the specified maximum rate. Thus, while the read-rate might exceed the maximum for a short interval, the average tends towards the specified maximum, overall.

    To build an instance, call builder().

    Inspired by Apache HBase's class of the same name.

    Since:
    2.16.0
    See Also:
    ThrottledInputStream.Builder
    • Field Detail

      • maxBytesPerSecond

        private final double maxBytesPerSecond
      • startTime

        private final long startTime
      • totalSleepDuration

        private java.time.Duration totalSleepDuration
    • Constructor Detail

      • ThrottledInputStream

        private ThrottledInputStream​(ThrottledInputStream.Builder builder)
                              throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • toSleepMillis

        static long toSleepMillis​(long bytesRead,
                                  long elapsedMillis,
                                  double maxBytesPerSec)
      • beforeRead

        protected void beforeRead​(int n)
                           throws java.io.IOException
        Description copied from class: ProxyInputStream
        Invoked by the read methods before the call is proxied. The number of bytes that the caller wanted to read (1 for the ProxyInputStream.read() method, buffer length for ProxyInputStream.read(byte[]), etc.) is given as an argument.

        Subclasses can override this method to add common pre-processing functionality without having to override all the read methods. The default implementation does nothing.

        Note this method is not called from ProxyInputStream.skip(long) or ProxyInputStream.reset(). You need to explicitly override those methods if you want to add pre-processing steps also to them.

        Overrides:
        beforeRead in class ProxyInputStream
        Parameters:
        n - number of bytes that the caller asked to be read.
        Throws:
        java.io.IOException - if the pre-processing fails in a subclass.
      • getBytesPerSecond

        private long getBytesPerSecond()
        Gets the read-rate from this stream, since creation. Calculated as bytesRead/elapsedTimeSinceStart.
        Returns:
        Read rate, in bytes/sec.
      • getMaxBytesPerSecond

        double getMaxBytesPerSecond()
      • getSleepMillis

        private long getSleepMillis()
      • getTotalSleepDuration

        java.time.Duration getTotalSleepDuration()
        Gets the total duration spent in sleep.
        Returns:
        Duration spent in sleep.
      • throttle

        private void throttle()
                       throws java.io.InterruptedIOException
        Throws:
        java.io.InterruptedIOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object