@ThreadSafety(level=NOT_THREADSAFE) public final class RateLimitedOutputStream extends java.io.OutputStream
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 and 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.
|
Modifier and Type | Method and 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.
|
public RateLimitedOutputStream(@NotNull java.io.OutputStream wrappedStream, int maxBytesPerSecond, boolean autoFlush)
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.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
java.io.IOException
- If a problem is encountered while closing the wrapped
output stream.public void write(int b) throws java.io.IOException
write
in class java.io.OutputStream
b
- The byte of data to be written. Only the least significant
eight bits will be written.java.io.IOException
- If a problem is encountered while writing to the
wrapped stream.public void write(@NotNull byte[] b) throws java.io.IOException
write
in class java.io.OutputStream
b
- The byte array containing the data to be written. It must not
be null
.java.io.IOException
- If a problem is encountered while writing to the
wrapped stream.public void write(@NotNull byte[] b, int offset, int length) throws java.io.IOException
write
in class java.io.OutputStream
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.java.io.IOException
- If a problem is encountered while writing to the
wrapped stream.public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.OutputStream
java.io.IOException
- If a problem is encountered while flushing the
wrapped stream.