ccs.utils
Class ThrottledInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by ccs.utils.ThrottledInputStream
All Implemented Interfaces:
java.io.Closeable

public class ThrottledInputStream
extends java.io.FilterInputStream

A stream with a settable maximum rate. Primarily intended for testing purposes, but could be also useful for handling congestion. MT-UNSAFE.


Field Summary
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
ThrottledInputStream(java.io.InputStream in)
          A ThrottledInputStream with an infinite ration.
ThrottledInputStream(java.io.InputStream in, int ration)
          A ThrottledInputStream with a fixed ration.
 
Method Summary
 int available()
           
 int getRation()
           
 int read()
           
 int read(byte[] wad, int off, int len)
           
 void setRation(int ration)
          Set the byte ration.
 long skip(long n)
           
 
Methods inherited from class java.io.FilterInputStream
close, mark, markSupported, read, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThrottledInputStream

public ThrottledInputStream(java.io.InputStream in)
A ThrottledInputStream with an infinite ration.

Parameters:
in - The underlying stream.

ThrottledInputStream

public ThrottledInputStream(java.io.InputStream in,
                            int ration)
A ThrottledInputStream with a fixed ration. Every second, it gets a new ration: within that second, you can only read or skip whatever's left of your ration. An attempt to read when you have zero ration left will block until the next ration interval. Unused rations cannot be carried forward. Note that this rationing is on top of whatever limitations the underlying stream has: you'll only get the behaviour stated here if the underlying stream can always satisfy fully whatever demands you make.

Parameters:
in - The underlying stream to read from.
ration - The ration per second. If zero, there is no rationing, and the stream is transparent.
Method Detail

setRation

public void setRation(int ration)
Set the byte ration. Up to this number of bytes may be read in a second.

Parameters:
ration - The new ration. Zero disables the rationing and allows as much data to be read as the underlying stream will permit.

getRation

public int getRation()

available

public int available()
              throws java.io.IOException
Overrides:
available in class java.io.FilterInputStream
Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Overrides:
read in class java.io.FilterInputStream
Throws:
java.io.IOException

read

public int read(byte[] wad,
                int off,
                int len)
         throws java.io.IOException
Overrides:
read in class java.io.FilterInputStream
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException
Overrides:
skip in class java.io.FilterInputStream
Throws:
java.io.IOException