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 anInputStreamimplementation that uses aFixedRateBarrierto 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 intavailable()Retrieves 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.booleanmarkSupported()intread()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.
-
-
-
Constructor Detail
-
RateLimitedInputStream
public RateLimitedInputStream(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:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein 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:
readin 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(byte[] b) throws java.io.IOException
Reads data from the wrapped input stream into the provided array.- Overrides:
readin 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(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:
readin 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:
availablein 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 thisInputStreamimplementation supports the use of themark(int)andreset()methods. This implementation will support those methods if the wrapped stream supports them.- Overrides:
markSupportedin classjava.io.InputStream- Returns:
trueif thisInputStreamsupports themarkandresetmethods, orfalseif 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:
markin 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:
resetin 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.
-
-