public class ThroughputLimitInputStream
extends java.io.InputStream
#setThroughputLimit(long)
is called with a more permissive bytes per second value (different from 0),
yielding to more bytes available for the current second.
Setting the throughput limit to 0 effectively blocks all read and skip calls indefinitely.
Any calls to the read or skip methods will lock, the only way to remove this lock being to call the
#setThroughputLimit(long)
method with a value different from 0 from another thread.
Setting the throughput limit to -1 or any other negative values will disable any limit and make this ThroughputLimitInputStream behave just like a normal InputStream.
Finally, the setUnderlyingInputStream(java.io.InputStream)
method allows to use the
same ThroughputLimitInputStream instance for multiple InputStream instances, keeping the bytes count for the
current second intact and thus the throughput limit stable. This does not hold true if a new ThroughputLimitInputStream
is created for each InputStream, the bytes count for the current second starting at 0.
Constructor and Description |
---|
ThroughputLimitInputStream(java.io.InputStream in)
Creates a new ThroughputLimitInputStream with no initial throughput limit (-1 value).
|
ThroughputLimitInputStream(java.io.InputStream in,
long bytesPerSecond)
Creates a new ThroughputLimitInputStream with an initial throughput limit.
|
Modifier and Type | Method and Description |
---|---|
int |
available() |
void |
close() |
void |
mark(int i) |
boolean |
markSupported() |
int |
read() |
int |
read(byte[] bytes) |
int |
read(byte[] bytes,
int off,
int len) |
void |
reset() |
void |
setUnderlyingInputStream(java.io.InputStream in)
Changes the underlying InputStream which data is read from, keeping the bytes count for the current second intact.
|
long |
skip(long l) |
public ThroughputLimitInputStream(java.io.InputStream in)
in
- underlying stream that is used to read data frompublic ThroughputLimitInputStream(java.io.InputStream in, long bytesPerSecond)
in
- underlying stream that is used to read data frombytesPerSecond
- initial throughput limit in bytes per second#setThroughputLimit(long)
public void setUnderlyingInputStream(java.io.InputStream in)
Note: the existing underlying InputStream will not be closed, the close()
method must be called prior
to calling this method.
in
- the new InputStream to read data frompublic int read() throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public int read(byte[] bytes) throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public int read(byte[] bytes, int off, int len) throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public long skip(long l) throws java.io.IOException
skip
in class java.io.InputStream
java.io.IOException
public int available() throws java.io.IOException
available
in class java.io.InputStream
java.io.IOException
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
public void mark(int i)
mark
in class java.io.InputStream
public void reset() throws java.io.IOException
reset
in class java.io.InputStream
java.io.IOException
public boolean markSupported()
markSupported
in class java.io.InputStream