I have something akin to the following:
public class X extends Thread{
BufferedInputStream in = (BufferedInputStream) System.in;
public void run() {
while (true) {
try {
while (in.available() > 0) {
// interesting stuff here
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
… which largely works, however sometimes I’ll start seeing the following in stderr (seems to repeat incessantly once it happens – I’m guessing the application eventually crashes once this starts occurring):
java.io.IOException: Illegal seek
at java.io.FileInputStream.available(Native Method)
at java.io.BufferedInputStream.available(BufferedInputStream.java:381)
at compactable.sqlpp.X.run(X.java:40)
… and I have no clue what causes this. Honestly stumped. Any ideas from the masses on how this could happen ?
Any / all useful suggestions gratefully received 🙂
If the stream has been closed you can get an IOException calling available.
Also, available() does not tell you how much is left to read of the stream or if the stream is empty, it only tells how much you can read without blocking (basically waiting for more to be put into the stream). What you want is to read until your read returns -1.
or