Package com.unboundid.util
Class RateLimitedInputStream
- java.lang.Object
-
- java.io.InputStream
-
- com.unboundid.util.RateLimitedInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
@ThreadSafety(level=NOT_THREADSAFE) public final class RateLimitedInputStream extends java.io.InputStream
This class provides anInputStream
implementation that uses aFixedRateBarrier
to impose an upper bound on the rate (in bytes per second) at which data can be read from a wrappedInputStream
.
-
-
Constructor Summary
Constructors Constructor Description RateLimitedInputStream(java.io.InputStream wrappedStream, int maxBytesPerSecond)
Creates a new instance of this rate-limited input stream that wraps the provided input stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.
-
-
-
Constructor Detail
-
RateLimitedInputStream
public RateLimitedInputStream(@NotNull java.io.InputStream wrappedStream, int maxBytesPerSecond)
Creates a new instance of this rate-limited input stream that wraps the provided input stream.- Parameters:
wrappedStream
- The input stream from which the data will actually be read. It must not benull
.maxBytesPerSecond
- The maximum number of bytes per second that can be read using this input stream. It must be greater than zero.
-
-
Method Detail
-
close
public void close() throws java.io.IOException
Closes this input stream and the wrapped stream.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.InputStream
- Throws:
java.io.IOException
- If a problem is encountered while closing the wrapped input stream.
-
read
public int read() throws java.io.IOException
Reads a single byte of input from the wrapped input stream.- Specified by:
read
in classjava.io.InputStream
- Returns:
- The byte that was read, or -1 if the end of the input stream has been reached.
- Throws:
java.io.IOException
- If a problem is encountered while attempting to read data from the underlying input stream.
-
read
public int read(@NotNull byte[] b) throws java.io.IOException
Reads data from the wrapped input stream into the provided array.- Overrides:
read
in classjava.io.InputStream
- Parameters:
b
- The array into which the data will be placed.- Returns:
- The number of bytes that were read, or -1 if the end of the input stream has been reached.
- Throws:
java.io.IOException
- If a problem is encountered while attempting to read data from the underlying input stream.
-
read
public int read(@NotNull byte[] b, int offset, int length) throws java.io.IOException
Reads data from the wrapped input stream into the specified portion of the provided array.- Overrides:
read
in classjava.io.InputStream
- Parameters:
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.- Returns:
- The number of bytes that were read, or -1 if the end of the input stream has been reached.
- Throws:
java.io.IOException
- If a problem is encountered while attempting to read data from the underlying input stream.
-
available
public int available() throws java.io.IOException
Retrieves the number of bytes that are immediately available to be read, if the wrapped stream supports this operation.- Overrides:
available
in classjava.io.InputStream
- Returns:
- The number of bytes that are immediately available to be read, or zero if there are no bytes to be read, if the end of the input stream has been reached, or if the wrapped input stream does not support this operation.
- Throws:
java.io.IOException
-
markSupported
public boolean markSupported()
Indicates whether thisInputStream
implementation supports the use of themark(int)
andreset()
methods. This implementation will support those methods if the wrapped stream supports them.- Overrides:
markSupported
in classjava.io.InputStream
- Returns:
true
if thisInputStream
supports themark
andreset
methods, orfalse
if not.
-
mark
public 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. fun- Overrides:
mark
in classjava.io.InputStream
- Parameters:
readLimit
- The maximum number of bytes expected to be read before a call to thereset()
method before the mark will no longer be honored.
-
reset
public void reset() throws java.io.IOException
Attempts to reset the position of this input stream to the last mark position.- Overrides:
reset
in classjava.io.InputStream
- Throws:
java.io.IOException
- If the input stream cannot be repositioned to the marked location, or if no mark has been set.
-
-