Class ThrottledInputStream
java.lang.Object
java.io.InputStream
org.apache.hadoop.hbase.io.hadoopbackport.ThrottledInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
The ThrottleInputStream 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 given short interval, the
average tends towards the specified maximum, overall.)
-
Field Summary
Modifier and TypeFieldDescriptionprivate long
private final long
private final InputStream
private final long
private long
-
Constructor Summary
ConstructorDescriptionThrottledInputStream
(InputStream rawStream) ThrottledInputStream
(InputStream rawStream, long maxBytesPerSec) -
Method Summary
Modifier and TypeMethodDescriptionprivate long
(package private) static long
calSleepTimeMs
(long bytesRead, long maxBytesPerSec, long elapsed) void
close()
long
Getter for the read-rate from this stream, since creation.long
Getter for the number of bytes read from this stream, since creation.long
Getter the total time spent in sleep.int
read()
int
read
(byte[] b) int
read
(byte[] b, int off, int len) int
read
(long position, byte[] buffer, int offset, int length) Read bytes starting from the specified position.private void
throttle()
toString()
Methods inherited from class java.io.InputStream
available, mark, markSupported, reset, skip
-
Field Details
-
rawStream
-
maxBytesPerSec
-
startTime
-
bytesRead
-
totalSleepTime
-
-
Constructor Details
-
ThrottledInputStream
-
ThrottledInputStream
-
-
Method Details
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
read
Read bytes starting from the specified position. This requires rawStream is an instance ofPositionedReadable
.- Returns:
- the number of bytes read
- Throws:
IOException
-
calSleepTimeMs
-
calSleepTimeMs
-
throttle
- Throws:
InterruptedIOException
-
getTotalBytesRead
Getter for the number of bytes read from this stream, since creation.- Returns:
- The number of bytes.
-
getBytesPerSec
Getter for the read-rate from this stream, since creation. Calculated as bytesRead/elapsedTimeSinceStart.- Returns:
- Read rate, in bytes/sec.
-
getTotalSleepTime
Getter the total time spent in sleep.- Returns:
- Number of milliseconds spent in sleep.
-
toString
-