public class DelayedInputStream extends InputStream implements TickListener, DelayShaper, Runnable
InputStream instance with a delay real the transferred
bytes.
The stream uses a TickSource as a source of timing information and
delays the reception of data by a certain number of ticks.| Constructor and Description |
|---|
DelayedInputStream(TickSource t,
InputStream real,
long delay,
int bufferSize)
Constructs a new
DelayedInputStream with the given parameters. |
| Modifier and Type | Method and Description |
|---|---|
int |
available() |
void |
close() |
InputStream |
getBaseStream()
Returns the underlying
InputStream. |
int |
getBufferSize()
Returns the buffer size, real bytes
|
long |
getDelay()
Returns the current delay in ticks.
|
TickSource |
getTickSource()
Returns the
TickSource this stream uses. |
int |
read() |
int |
read(byte[] b) |
int |
read(byte[] b,
int off,
int len) |
void |
run() |
void |
setDelay(long delay)
Sets a new delay in ticks.
|
void |
setEofDetection(boolean eofDetection)
Sets whether this
DelayedInputStream should employ EOF detection. |
void |
setNonBlocking(boolean nonBlocking)
Sets the non-blocking flag.
|
void |
tick(long tick)
Called when a tick event is fired.
|
mark, markSupported, reset, skippublic DelayedInputStream(TickSource t, InputStream real, long delay, int bufferSize)
DelayedInputStream with the given parameters.t - the tick source to usereal - the underlying InputStream to read data fromdelay - the delay (real ticks) to introduce to databufferSize - the buffer size to use. The implementation has to store read
bytes real an intermediate buffer. The buffer must be large
enough to store the data. Note that data which cannot be stored
into the buffer because of its overflow will have a higher
delay, which is why the buffer should be significantly higher
than the expected data arrivel rate (times the expected interval
of reading from this stream).public int read()
throws IOException
read in class InputStreamIOExceptionpublic void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class InputStreamIOExceptionpublic int read(byte[] b,
int off,
int len)
throws IOException
read in class InputStreamIOExceptionpublic int read(byte[] b)
throws IOException
read in class InputStreamIOExceptionpublic void tick(long tick)
TickListenerLong.MAX_VALUE is followed by
Long.MIN_VALUE.
Furthermore, it is guaranteed that ticks do not overlap each other, ie. no
two threads will enter the same object's TickListener.tick(long)
at the same time.tick in interface TickListenertick - the tick numberpublic int available()
throws IOException
available in class InputStreamIOExceptionpublic void setDelay(long delay)
DelayShapersetDelay in interface DelayShaperdelay - the new delay in tickspublic long getDelay()
DelayShapergetDelay in interface DelayShaperpublic int getBufferSize()
public InputStream getBaseStream()
InputStream.public TickSource getTickSource()
TickSource this stream uses.public void setNonBlocking(boolean nonBlocking)
nonBlocking - whether the stream should be in non-blocking mode (see
RateLimitInputStream).public void setEofDetection(boolean eofDetection)
DelayedInputStream should employ EOF detection.
If this flag is set to true, this stream starts showing
non-deterministic behavior (in general), since a new thread is spawned for
continuous EOF detection which might or might not wake up before a certain
tick to report a new byte.
Without EOF detection, EOF is not reported to the caller at all.
If this flag is set to false while an EOF detector thread is
running, the thread might finish at some undefined point in future, ie.
setting this flag to false only prevents new EOF detector threads from
spawning.eofDetection - if EOF detection should be employed (impllies non-deterministisc
behavior)Copyright © 2015. All rights reserved.