Class RateLimitedInputStream

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

    @ThreadSafety(level=NOT_THREADSAFE)
    public final class RateLimitedInputStream
    extends java.io.InputStream
    This class provides an InputStream implementation that uses a FixedRateBarrier to impose an upper bound on the rate (in bytes per second) at which data can be read from a wrapped InputStream.
    • Constructor Summary

      Constructors 
      Constructor Description
      RateLimitedInputStream​(java.io.InputStream wrappedStream, int maxBytesPerSecond)
      Creates a new instance of this rate-limited input stream that wraps the provided input stream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()
      Retrieves the number of bytes that are immediately available to be read, if the wrapped stream supports this operation.
      void close()
      Closes this input stream and the wrapped stream.
      void mark​(int readLimit)
      Attempts to mark the current position in the wrapped input stream so that it can optionally be reset after some amount of data has been read.
      boolean markSupported()
      Indicates whether this InputStream implementation supports the use of the mark(int) and reset() methods.
      int read()
      Reads a single byte of input from the wrapped input stream.
      int read​(byte[] b)
      Reads data from the wrapped input stream into the provided array.
      int read​(byte[] b, int offset, int length)
      Reads data from the wrapped input stream into the specified portion of the provided array.
      void reset()
      Attempts to reset the position of this input stream to the last mark position.
      • Methods inherited from class java.io.InputStream

        nullInputStream, readAllBytes, readNBytes, readNBytes, skip, transferTo
      • Methods inherited from class java.lang.Object

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

      • RateLimitedInputStream

        public RateLimitedInputStream​(@NotNull
                                      java.io.InputStream wrappedStream,
                                      int maxBytesPerSecond)
        Creates a new instance of this rate-limited input stream that wraps the provided input stream.
        Parameters:
        wrappedStream - The input stream from which the data will actually be read. It must not be null.
        maxBytesPerSecond - The maximum number of bytes per second that can be read using this input stream. It must be greater than zero.
    • Method Detail

      • close

        public void close()
                   throws java.io.IOException
        Closes this input stream and the wrapped stream.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
        Throws:
        java.io.IOException - If a problem is encountered while closing the wrapped input stream.
      • read

        public int read()
                 throws java.io.IOException
        Reads a single byte of input from the wrapped input stream.
        Specified by:
        read in class java.io.InputStream
        Returns:
        The byte that was read, or -1 if the end of the input stream has been reached.
        Throws:
        java.io.IOException - If a problem is encountered while attempting to read data from the underlying input stream.
      • read

        public int read​(@NotNull
                        byte[] b)
                 throws java.io.IOException
        Reads data from the wrapped input stream into the provided array.
        Overrides:
        read in class java.io.InputStream
        Parameters:
        b - The array into which the data will be placed.
        Returns:
        The number of bytes that were read, or -1 if the end of the input stream has been reached.
        Throws:
        java.io.IOException - If a problem is encountered while attempting to read data from the underlying input stream.
      • read

        public int read​(@NotNull
                        byte[] b,
                        int offset,
                        int length)
                 throws java.io.IOException
        Reads data from the wrapped input stream into the specified portion of the provided array.
        Overrides:
        read in class java.io.InputStream
        Parameters:
        b - The array into which the data will be placed.
        offset - The index into the provided array at which the data should start being added.
        length - The maximum number of bytes to be added into the array.
        Returns:
        The number of bytes that were read, or -1 if the end of the input stream has been reached.
        Throws:
        java.io.IOException - If a problem is encountered while attempting to read data from the underlying input stream.
      • available

        public int available()
                      throws java.io.IOException
        Retrieves the number of bytes that are immediately available to be read, if the wrapped stream supports this operation.
        Overrides:
        available in class java.io.InputStream
        Returns:
        The number of bytes that are immediately available to be read, or zero if there are no bytes to be read, if the end of the input stream has been reached, or if the wrapped input stream does not support this operation.
        Throws:
        java.io.IOException
      • markSupported

        public boolean markSupported()
        Indicates whether this InputStream implementation supports the use of the mark(int) and reset() methods. This implementation will support those methods if the wrapped stream supports them.
        Overrides:
        markSupported in class java.io.InputStream
        Returns:
        true if this InputStream supports the mark and reset methods, or false if not.
      • mark

        public void mark​(int readLimit)
        Attempts to mark the current position in the wrapped input stream so that it can optionally be reset after some amount of data has been read. fun
        Overrides:
        mark in class java.io.InputStream
        Parameters:
        readLimit - The maximum number of bytes expected to be read before a call to the reset() method before the mark will no longer be honored.
      • reset

        public void reset()
                   throws java.io.IOException
        Attempts to reset the position of this input stream to the last mark position.
        Overrides:
        reset in class java.io.InputStream
        Throws:
        java.io.IOException - If the input stream cannot be repositioned to the marked location, or if no mark has been set.