Class RateLimitedOutputStream

java.lang.Object
java.io.OutputStream
com.unboundid.util.RateLimitedOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

This class provides an OutputStream implementation that uses a FixedRateBarrier to impose an upper bound on the rate (in bytes per second) at which data can be written to a wrapped OutputStream.
  • Constructor Summary

    Constructors
    Constructor
    Description
    RateLimitedOutputStream(OutputStream wrappedStream, int maxBytesPerSecond, boolean autoFlush)
    Creates a new instance of this rate-limited output stream that wraps the provided output stream.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes this output stream and the wrapped stream.
    void
    Flushes the contents of the wrapped stream.
    void
    write(byte[] b)
    Writes the contents of the provided array to the wrapped output stream.
    void
    write(byte[] b, int offset, int length)
    Writes the contents of the specified portion of the provided array to the wrapped output stream.
    void
    write(int b)
    Writes a single byte of data to the wrapped output stream.

    Methods inherited from class java.io.OutputStream

    nullOutputStream

    Methods inherited from class java.lang.Object

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

    • RateLimitedOutputStream

      public RateLimitedOutputStream(@NotNull OutputStream wrappedStream, int maxBytesPerSecond, boolean autoFlush)
      Creates a new instance of this rate-limited output stream that wraps the provided output stream.
      Parameters:
      wrappedStream - The output stream to which the data will actually be written. It must not be null.
      maxBytesPerSecond - The maximum number of bytes per second that can be written using this output stream. It must be greater than zero.
      autoFlush - Indicates whether to automatically flush the wrapped output stream after each write.
  • Method Details

    • close

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

      public void write(int b) throws IOException
      Writes a single byte of data to the wrapped output stream.
      Specified by:
      write in class OutputStream
      Parameters:
      b - The byte of data to be written. Only the least significant eight bits will be written.
      Throws:
      IOException - If a problem is encountered while writing to the wrapped stream.
    • write

      public void write(@NotNull byte[] b) throws IOException
      Writes the contents of the provided array to the wrapped output stream.
      Overrides:
      write in class OutputStream
      Parameters:
      b - The byte array containing the data to be written. It must not be null.
      Throws:
      IOException - If a problem is encountered while writing to the wrapped stream.
    • write

      public void write(@NotNull byte[] b, int offset, int length) throws IOException
      Writes the contents of the specified portion of the provided array to the wrapped output stream.
      Overrides:
      write in class OutputStream
      Parameters:
      b - The byte array containing the data to be written. It must not be null.
      offset - The position in the provided array at which the data to write begins. It must be greater than or equal to zero and less than the length of the provided array.
      length - The number of bytes to be written. It must not be negative, and the sum of offset and length must be less than or equal to the length of the provided array.
      Throws:
      IOException - If a problem is encountered while writing to the wrapped stream.
    • flush

      public void flush() throws IOException
      Flushes the contents of the wrapped stream.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException - If a problem is encountered while flushing the wrapped stream.