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:
java.io.Closeable,java.lang.AutoCloseable
public final class ThrottledInputStream extends CountingInputStream
Provides bandwidth throttling on an InputStream as a filter input stream. The throttling examines the number of bytes read from the underlying InputStream, and sleeps for a time interval if the byte-transfer is found to exceed the specified maximum rate. 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, call
builder().Inspired by Apache HBase's class of the same name.
- Since:
- 2.16.0
- See Also:
ThrottledInputStream.Builder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classThrottledInputStream.BuilderBuilds a newThrottledInputStream.-
Nested classes/interfaces inherited from class org.apache.commons.io.input.ProxyInputStream
ProxyInputStream.AbstractBuilder<T,B extends AbstractStreamBuilder<T,B>>
-
-
Field Summary
Fields Modifier and Type Field Description private doublemaxBytesPerSecondprivate longstartTimeprivate java.time.DurationtotalSleepDuration
-
Constructor Summary
Constructors Modifier Constructor Description privateThrottledInputStream(ThrottledInputStream.Builder builder)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidbeforeRead(int n)Invoked by thereadmethods before the call is proxied.static ThrottledInputStream.Builderbuilder()Constructs a newThrottledInputStream.Builder.private longgetBytesPerSecond()Gets the read-rate from this stream, since creation.(package private) doublegetMaxBytesPerSecond()Gets the maximum bytes per second.(package private) longgetSleepMillis()Gets the number of milliseconds to sleep to match to the maximum bytes per second.(package private) java.time.DurationgetTotalSleepDuration()Gets the total duration spent in sleep.private voidthrottle()(package private) static longtoSleepMillis(long bytesRead, long elapsedMillis, double maxBytesPerSec)java.lang.StringtoString()-
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, checkOpen, close, handleIOException, isClosed, mark, markSupported, read, read, read, reset, setReference, unwrap
-
-
-
-
Constructor Detail
-
ThrottledInputStream
private ThrottledInputStream(ThrottledInputStream.Builder builder) throws java.io.IOException
- Throws:
java.io.IOException
-
-
Method Detail
-
builder
public static ThrottledInputStream.Builder builder()
Constructs a newThrottledInputStream.Builder.- Returns:
- a new
ThrottledInputStream.Builder.
-
toSleepMillis
static long toSleepMillis(long bytesRead, long elapsedMillis, double maxBytesPerSec)
-
beforeRead
protected void beforeRead(int n) throws java.io.IOExceptionDescription copied from class:ProxyInputStreamInvoked by thereadmethods 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:
beforeReadin classProxyInputStream- Parameters:
n- number of bytes that the caller asked to be read.- Throws:
java.io.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.
-
getMaxBytesPerSecond
double getMaxBytesPerSecond()
Gets the maximum bytes per second.Package private for testing.
- Returns:
- The maximum bytes per second.
-
getSleepMillis
long getSleepMillis()
Gets the number of milliseconds to sleep to match to the maximum bytes per second.Package private for testing.
- Returns:
- the number of milliseconds to sleep to match to the maximum bytes per second.
-
getTotalSleepDuration
java.time.Duration getTotalSleepDuration()
Gets the total duration spent in sleep.Package private for testing
- Returns:
- Duration spent in sleep.
-
throttle
private void throttle() throws java.io.InterruptedIOException- Throws:
java.io.InterruptedIOException
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-