Class RateLimitedOutputStream

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

    @ThreadSafety(level=NOT_THREADSAFE)
    public final class RateLimitedOutputStream
    extends java.io.OutputStream
    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​(java.io.OutputStream wrappedStream, int maxBytesPerSecond, boolean autoFlush)
      Creates a new instance of this rate-limited output stream that wraps the provided output stream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes this output stream and the wrapped stream.
      void flush()
      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 Detail

      • RateLimitedOutputStream

        public RateLimitedOutputStream​(@NotNull
                                       java.io.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 Detail

      • close

        public void close()
                   throws java.io.IOException
        Closes this output 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.OutputStream
        Throws:
        java.io.IOException - If a problem is encountered while closing the wrapped output stream.
      • write

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

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

        public void write​(@NotNull
                          byte[] b,
                          int offset,
                          int length)
                   throws java.io.IOException
        Writes the contents of the specified portion of the provided array to the wrapped output stream.
        Overrides:
        write in class java.io.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:
        java.io.IOException - If a problem is encountered while writing to the wrapped stream.
      • flush

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