I am trying to mock a connection with an Inputstream:
private static class MockInputStream extends InputStream {
@Override
public int read() throws IOException {
//how do I block here until data is available?
return someData;
}
}
The reason I am doing this is to simulate a connection between my device and a microcontroller for testing purposes.
My problem is getting the InputStream to 'block' when read is called. Do I just sleep indefinitely until my thread is interrupted when data is available?
private static class MockInputStream extends InputStream {
private int someData;
private Thread blockedThread;
public void setData(int i) {
this.someData = i;
blockedThread.interrupt();
}
@Override
public int read() throws IOException {
blockThread = Thread.currentThread();
try {
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
return someData;
}
}
}
Obviously, the read() is being called from a separate thread
Aucun commentaire:
Enregistrer un commentaire