Package com.unboundid.util
Class RateLimitedInputStream
java.lang.Object
java.io.InputStream
com.unboundid.util.RateLimitedInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
This class provides an
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 Summary
ConstructorsConstructorDescriptionRateLimitedInputStream(InputStream wrappedStream, int maxBytesPerSecond) Creates a new instance of this rate-limited input stream that wraps the provided input stream. -
Method Summary
Modifier and TypeMethodDescriptionintRetrieves the number of bytes that are immediately available to be read, if the wrapped stream supports this operation.voidclose()Closes this input stream and the wrapped stream.voidmark(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.booleanintread()Reads a single byte of input from the wrapped input stream.intread(byte[] b) Reads data from the wrapped input stream into the provided array.intread(byte[] b, int offset, int length) Reads data from the wrapped input stream into the specified portion of the provided array.voidreset()Attempts to reset the position of this input stream to the last mark position.Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skip, skipNBytes, transferTo
-
Constructor Details
-
RateLimitedInputStream
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 Details
-
close
Closes this input stream and the wrapped stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- If a problem is encountered while closing the wrapped input stream.
-
read
Reads a single byte of input from the wrapped input stream.- Specified by:
readin classInputStream- Returns:
- The byte that was read, or -1 if the end of the input stream has been reached.
- Throws:
IOException- If a problem is encountered while attempting to read data from the underlying input stream.
-
read
Reads data from the wrapped input stream into the provided array.- Overrides:
readin classInputStream- 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:
IOException- If a problem is encountered while attempting to read data from the underlying input stream.
-
read
Reads data from the wrapped input stream into the specified portion of the provided array.- Overrides:
readin classInputStream- 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:
IOException- If a problem is encountered while attempting to read data from the underlying input stream.
-
available
Retrieves the number of bytes that are immediately available to be read, if the wrapped stream supports this operation.- Overrides:
availablein classInputStream- 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:
IOException
-
markSupported
Indicates whether thisInputStreamimplementation supports the use of themark(int)andreset()methods. This implementation will support those methods if the wrapped stream supports them.- Overrides:
markSupportedin classInputStream- Returns:
trueif thisInputStreamsupports themarkandresetmethods, orfalseif not.
-
mark
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:
markin classInputStream- 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
Attempts to reset the position of this input stream to the last mark position.- Overrides:
resetin classInputStream- Throws:
IOException- If the input stream cannot be repositioned to the marked location, or if no mark has been set.
-