I want to do something like this:
// Implement an interruptible read
for(;;) {
if (input.available() > 0)
buffer.append(input.read());
else if (input.eof())
return buffer;
else
Thread.sleep(250);
}
If I didn’t care about blocking, I would have done this:
for(;;) {
c = input.read();
if (c != -1)
buffer.append(c);
return buffer;
}
But I do care, so I need to use available(), so how can I determine EOF?
You could always use the
NIOlibrary instead, as that provides non-blocking IO (as the name suggests). There’s an Oracle blog post about IO vs NIO: Here.Alternatively there are some code examples provided Here about setting timeout parameters on the read from an
InputStream