org.apache.hadoop.hbase.io.hadoopbackport
Class ThrottledInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.apache.hadoop.hbase.io.hadoopbackport.ThrottledInputStream
All Implemented Interfaces:
Closeable

public class ThrottledInputStream
extends InputStream

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.)


Constructor Summary
ThrottledInputStream(InputStream rawStream)
           
ThrottledInputStream(InputStream rawStream, long maxBytesPerSec)
           
 
Method Summary
 void close()
           
 long getBytesPerSec()
          Getter for the read-rate from this stream, since creation.
 long getTotalBytesRead()
          Getter for the number of bytes read from this stream, since creation.
 long getTotalSleepTime()
          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.
 String toString()
           
 
Methods inherited from class java.io.InputStream
available, mark, markSupported, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ThrottledInputStream

public ThrottledInputStream(InputStream rawStream)

ThrottledInputStream

public ThrottledInputStream(InputStream rawStream,
                            long maxBytesPerSec)
Method Detail

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException

read

public int read()
         throws IOException
Specified by:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

read

public int read(long position,
                byte[] buffer,
                int offset,
                int length)
         throws IOException
Read bytes starting from the specified position. This requires rawStream is an instance of PositionedReadable.

Parameters:
position -
buffer -
offset -
length -
Returns:
the number of bytes read
Throws:
IOException

getTotalBytesRead

public long getTotalBytesRead()
Getter for the number of bytes read from this stream, since creation.

Returns:
The number of bytes.

getBytesPerSec

public long getBytesPerSec()
Getter for the read-rate from this stream, since creation. Calculated as bytesRead/elapsedTimeSinceStart.

Returns:
Read rate, in bytes/sec.

getTotalSleepTime

public long getTotalSleepTime()
Getter the total time spent in sleep.

Returns:
Number of milliseconds spent in sleep.

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2015 The Apache Software Foundation. All Rights Reserved.