Package org.apache.commons.io.input
Class ThrottledInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.apache.commons.io.input.ProxyInputStream
org.apache.commons.io.input.CountingInputStream
org.apache.commons.io.input.ThrottledInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
Provides bandwidth throttling on a specified InputStream. It is implemented as a wrapper on top of another InputStream instance. The throttling works by
examining the number of bytes read from the underlying InputStream from the beginning, and sleep()ing for a time interval if the byte-transfer is found
exceed the specified tolerable maximum. (Thus, while the read-rate might exceed the maximum for a short interval, the average tends towards the
specified maximum, overall.)
To build an instance, see ThrottledInputStream.Builder
Inspired by Apache HBase's class of the same name.
- Since:
- 2.16.0
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsFields inherited from class java.io.FilterInputStream
in
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
ThrottledInputStream
(InputStream proxy, long maxBytesPerSecond) -
Method Summary
Modifier and TypeMethodDescriptionprotected void
beforeRead
(int n) Invoked by theread
methods before the call is proxied.static ThrottledInputStream.Builder
builder()
Constructs a newThrottledInputStream.Builder
.private long
Gets the read-rate from this stream, since creation.private long
(package private) Duration
Gets the total duration spent in sleep.private void
throttle()
(package private) static long
toSleepMillis
(long bytesRead, long maxBytesPerSec, long elapsedMillis) toString()
Methods inherited from class org.apache.commons.io.input.CountingInputStream
afterRead, getByteCount, getCount, resetByteCount, resetCount, skip
Methods inherited from class org.apache.commons.io.input.ProxyInputStream
available, close, handleIOException, mark, markSupported, read, read, read, reset, unwrap
-
Field Details
-
maxBytesPerSecond
private final long maxBytesPerSecond -
startTime
private final long startTime -
totalSleepDuration
-
-
Constructor Details
-
ThrottledInputStream
-
-
Method Details
-
builder
Constructs a newThrottledInputStream.Builder
.- Returns:
- a new
ThrottledInputStream.Builder
.
-
toSleepMillis
static long toSleepMillis(long bytesRead, long maxBytesPerSec, long elapsedMillis) -
beforeRead
Description copied from class:ProxyInputStream
Invoked by theread
methods before the call is proxied. The number of bytes that the caller wanted to read (1 for theProxyInputStream.read()
method, buffer length forProxyInputStream.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)
orProxyInputStream.reset()
. You need to explicitly override those methods if you want to add pre-processing steps also to them.- Overrides:
beforeRead
in classProxyInputStream
- Parameters:
n
- number of bytes that the caller asked to be read.- Throws:
IOException
- if the pre-processing fails in a subclass.
-
getBytesPerSecond
private long getBytesPerSecond()Gets the read-rate from this stream, since creation. Calculated as bytesRead/elapsedTimeSinceStart.- Returns:
- Read rate, in bytes/sec.
-
getSleepMillis
private long getSleepMillis() -
getTotalSleepDuration
Duration getTotalSleepDuration()Gets the total duration spent in sleep.- Returns:
- Duration spent in sleep.
-
throttle
- Throws:
InterruptedIOException
-
toString
-