public final class ThrottledInputStream extends CountingInputStream
To build an instance, see ThrottledInputStream.Builder
Inspired by Apache HBase's class of the same name.
ThrottledInputStream.Builder| Modifier and Type | Class and Description |
|---|---|
static class |
ThrottledInputStream.Builder
Builds a new
ThrottledInputStream. |
| Modifier and Type | Field and Description |
|---|---|
private long |
maxBytesPerSecond |
private long |
startTime |
private java.time.Duration |
totalSleepDuration |
| Modifier | Constructor and Description |
|---|---|
private |
ThrottledInputStream(java.io.InputStream proxy,
long maxBytesPerSecond) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
beforeRead(int n)
Invoked by the
read methods before the call is proxied. |
static ThrottledInputStream.Builder |
builder()
Constructs a new
ThrottledInputStream.Builder. |
private long |
getBytesPerSecond()
Gets the read-rate from this stream, since creation.
|
private long |
getSleepMillis() |
(package private) java.time.Duration |
getTotalSleepDuration()
Gets the total duration spent in sleep.
|
private void |
throttle() |
(package private) static long |
toSleepMillis(long bytesRead,
long maxBytesPerSec,
long elapsedMillis) |
java.lang.String |
toString() |
afterRead, getByteCount, getCount, resetByteCount, resetCount, skipavailable, close, handleIOException, mark, markSupported, read, read, read, reset, unwrapprivate final long maxBytesPerSecond
private final long startTime
private java.time.Duration totalSleepDuration
private ThrottledInputStream(java.io.InputStream proxy,
long maxBytesPerSecond)
public static ThrottledInputStream.Builder builder()
ThrottledInputStream.Builder.ThrottledInputStream.Builder.static long toSleepMillis(long bytesRead,
long maxBytesPerSec,
long elapsedMillis)
protected void beforeRead(int n)
throws java.io.IOException
ProxyInputStreamread methods before the call is proxied. The number
of bytes that the caller wanted to read (1 for the ProxyInputStream.read()
method, buffer length for ProxyInputStream.read(byte[]), etc.) is given as
an argument.
Subclasses can override this method to add common pre-processing functionality without having to override all the read methods. The default implementation does nothing.
Note this method is not called from ProxyInputStream.skip(long) or
ProxyInputStream.reset(). You need to explicitly override those methods if
you want to add pre-processing steps also to them.
beforeRead in class ProxyInputStreamn - number of bytes that the caller asked to be read.java.io.IOException - if the pre-processing fails in a subclass.private long getBytesPerSecond()
private long getSleepMillis()
java.time.Duration getTotalSleepDuration()
private void throttle()
throws java.io.InterruptedIOException
java.io.InterruptedIOExceptionpublic java.lang.String toString()
toString in class java.lang.Object