@ThreadSafety(level=NOT_THREADSAFE) public final class RateLimitedInputStream extends java.io.InputStream
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 and Description |
---|
RateLimitedInputStream(java.io.InputStream wrappedStream,
int maxBytesPerSecond)
Creates a new instance of this rate-limited input stream that wraps the
provided input stream.
|
Modifier and Type | Method and Description |
---|---|
int |
available()
Retrieves the number of bytes that are immediately available to be read,
if the wrapped stream supports this operation.
|
void |
close()
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 |
markSupported()
|
int |
read()
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 |
reset()
Attempts to reset the position of this input stream to the last mark
position.
|
public RateLimitedInputStream(@NotNull java.io.InputStream wrappedStream, int maxBytesPerSecond)
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.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
- If a problem is encountered while closing the wrapped
input stream.public int read() throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
- If a problem is encountered while attempting to read
data from the underlying input stream.public int read(@NotNull byte[] b) throws java.io.IOException
read
in class java.io.InputStream
b
- The array into which the data will be placed.java.io.IOException
- If a problem is encountered while attempting to read
data from the underlying input stream.public int read(@NotNull byte[] b, int offset, int length) throws java.io.IOException
read
in class java.io.InputStream
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.java.io.IOException
- If a problem is encountered while attempting to read
data from the underlying input stream.public int available() throws java.io.IOException
available
in class java.io.InputStream
java.io.IOException
public boolean markSupported()
InputStream
implementation supports the use
of the mark(int)
and reset()
methods. This
implementation will support those methods if the wrapped stream supports
them.markSupported
in class java.io.InputStream
true
if this InputStream
supports the
mark
and reset
methods, or false
if not.public void mark(int readLimit)
mark
in class java.io.InputStream
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.public void reset() throws java.io.IOException
reset
in class java.io.InputStream
java.io.IOException
- If the input stream cannot be repositioned to the
marked location, or if no mark has been set.