I’m trying to create a InputStream class that behaves like a System.in InputStream but can be programatically added to (an endless InputStream that can be dynamically added to)
If you have trouble understanding what I mean, here’s what I have wrote and tried
public class QueuedInputStream extends InputStream {
private LinkedList<Character> list;
public QueuedInputStream() {
list = new LinkedList<Character>();
}
@Override
public int read() throws IOException {
while (list.isEmpty()) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int value = (byte)list.get(0).charValue();
list.remove();
return value;
}
public void queue(String s) {
char[] chars = s.toCharArray();
for(int i = 0; i < chars.length; i++) {
list.add(chars[i]);
}
}
}
Am I on the right track? Or am I completely wrong in trying to do this?
If you want me to explain more, feel free to ask
PipedInputStream:
ByteArrayInputStream: