You can do like this:
Construct a DelayQueue
with your simulated input plus the delay time.
Extend the BytArrayInputStream
and override the read()
method to read a DelayQueue
when read() is called.
EDIT: sample code (not fully implemented - am on a tel meeting)
public class DelayedString implements Delayed {
private final long delayInMillis;
private final String content;
public DelayedString(long delay, String content) {
this.delayInMillis = delay;
this.content = content;
}
public String getContent() {
return content;
}
public long getDelay(TimeUnit timeUnit) {
return TimeUnit.MILLISECONDS.convert(delayInMillis, timeUnit);
}
}
public class MyInputStream implements InputStream {
private ByteBuffer buffer = ByteBuffer.allocate(8192);
private final DelayQueue<DelayString> queue;
public MyInputStream(DelayQueue<DelayString> queue) {
this.queue = queue;
}
public int read() {
updateBuffer();
if (!buffer.isEmpty()) {
// deliver content inside buffer
}
}
public int read(char[] buffer, int count) {
updateBuffer();
// deliver content in byte buffer into buffer
}
protected void updateBuffer() {
for (DelayedString s = queue.peek(); s != null; ) {
if (buffer.capacity() > buffer.limit() + s.getContent().length()) {
s = queue.poll();
buffer.append(s.getContent());
} else {
break;
}
}
}
}