Package com.unboundid.util
Class RateLimitedOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- com.unboundid.util.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 anOutputStream
implementation that uses aFixedRateBarrier
to impose an upper bound on the rate (in bytes per second) at which data can be written to a wrappedOutputStream
.
-
-
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.
-
-
-
Constructor Detail
-
RateLimitedOutputStream
public 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.- Parameters:
wrappedStream
- The output stream to which the data will actually be written. It must not benull
.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 interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.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 classjava.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(byte[] b) throws java.io.IOException
Writes the contents of the provided array to the wrapped output stream.- Overrides:
write
in classjava.io.OutputStream
- Parameters:
b
- The byte array containing the data to be written. It must not benull
.- Throws:
java.io.IOException
- If a problem is encountered while writing to the wrapped stream.
-
write
public void write(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 classjava.io.OutputStream
- Parameters:
b
- The byte array containing the data to be written. It must not benull
.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 interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.OutputStream
- Throws:
java.io.IOException
- If a problem is encountered while flushing the wrapped stream.
-
-