public class RateLimitInputStreamextends InputStreamimplements RateShaper
InputStreamwith a limited rate of bytes. A stream has two blocking modes: blocking and non-blocking. If the stream is in non-blocking mode, any call that would cause the stream to block (ie. all read calls when no data is available) throw a
WouldBlockException. A stream has two boring modes: boring and non-boring. The boring flag modifies he behavior of
read(byte, int, int)and
read(byte). According to
InputStream, they method read between 1 and n bytes, where n is the size of the effective buffer. If the
RateLimitInputStreamis in boring mode, the methods block until all actually available bytes have been read, even if many ticks must be waited for the data to become available according to the set rate. In non-boring mode, the methods do not block and read only data which can be read immediately (except for the first byte which, according to the definition of
InputStream, causes blocking for all read methods). Note that blocking and boring are two different concept even though they seem similar. Non-blocking will cause methods to throw exceptions, where non-boring only means that read methods exit sooner in certain situations than in boring mode. Streams are non-boring by default.
|Constructor and Description|
Constructs a byte-rate-limited
|Modifier and Type||Method and Description|
This method complies with
Returns the underlying
Returns the bytes per tick (after prescaling) for this rate shaper.
Returns the current prescale value (ie.
Reads one byte from the stream.
Behaves the same as:
This method complies with
Sets the boring flag.
Sets a new byte rate per tick (after prescaling) for this rate shaper.
Sets the non-blocking flag.
Sets a new prescale value (ie.
public RateLimitInputStream(InputStream real, TickSource tickSource, int byteRate, int prescale)
InputStream. The rate is given using two numbers: bytes per tick, and a prescale. The prescale divides the incoming ticks by a constant number. A prescale of 1 means that each tick coming from the tick source is counting towards the rate, a prescale of 2 means every second, and so on. The prescale can be used to achieve low byte rates, ie. rates below 1 byte per tick. For example, a rate of 1 byte per tick and a prescale of 4 means that one byte is read after 4 ticks coming from the tick source.
real- The actual
InputStreamto read from
tickSource- The tick source
byteRate- The rate, ie. the limit of how many bytes per tick (after prescaling) should be readable from this stream
prescale- The prescaling, ie. the frequency of ticks to actually consider for rating.
public void setByteRate(int bytesPerTick)
public int getByteRate()
public int read() throws IOException
InputStreamor a rate block has been encountered, regardless of the stream's boring flag.
public int read(byte b, int off, int len) throws IOException
InputStream.read(byte, int, int)and imposes stronger postconditions. Reads up to
lenbytes from the stream. This method blocks for the first byte reagrdless of the stream's boring flag. For all subsequent bytes, the behavior depends on the boring flag:
InputStream.available()) have been read, even if this means waiting for the next tick.
InputStream.read(byte, int, int).
public int read(byte b) throws IOException
read(b, 0, b.length);
public int available() throws IOException
InputStream.available()and imposes stronger postconditions. The returned value is the number of bytes available for immediate reading. If there is no rate limitation in place (ie. the rate has not been reached for the current tick), the
InputStream.available()method of the underlying
InputStreamis called. If a rate limitation is in place, the number of bytes readable in the current tick is returned.
public void close() throws IOException
public void setBoring(boolean boring)
boring- whether the stream should be in boring mode (see
RateLimitInputStream(InputStream, TickSource, int, int)).
public void setNonBlocking(boolean nonBlocking)
nonBlocking- whether the stream should be in non-blocking mode (see
public InputStream getBaseStream()
public TickSource getTickSource()
TickSourcethis stream uses.
public int getPrescale()
Copyright © 2015. All rights reserved.