Class RateLimitedInputStream

java.lang.Object
java.io.InputStream
com.unboundid.util.RateLimitedInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

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(InputStream wrappedStream, int maxBytesPerSecond)
    Creates a new instance of this rate-limited input stream that wraps the provided input stream.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Retrieves the number of bytes that are immediately available to be read, if the wrapped stream supports this operation.
    void
    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
    Indicates whether this InputStream implementation supports the use of the mark(int) and reset() methods.
    int
    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
    Attempts to reset the position of this input stream to the last mark position.

    Methods inherited from class java.lang.Object

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

    • RateLimitedInputStream

      public RateLimitedInputStream(@NotNull 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 Details

    • close

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

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

      public int read(@NotNull byte[] b) throws IOException
      Reads data from the wrapped input stream into the provided array.
      Overrides:
      read in class 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:
      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 IOException
      Reads data from the wrapped input stream into the specified portion of the provided array.
      Overrides:
      read in class 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:
      IOException - If a problem is encountered while attempting to read data from the underlying input stream.
    • available

      public int available() throws IOException
      Retrieves the number of bytes that are immediately available to be read, if the wrapped stream supports this operation.
      Overrides:
      available in class 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:
      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 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 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 IOException
      Attempts to reset the position of this input stream to the last mark position.
      Overrides:
      reset in class InputStream
      Throws:
      IOException - If the input stream cannot be repositioned to the marked location, or if no mark has been set.