I am using a FileInputStream in a thread in Android java to read from a serial interface file. stream.read(buffer) blocks if there is not data waiting right now, if new data comes in again is continues. Data arrives every now and then, whenever something comes in the thread continues running. That's perfect! 

But when I want to terminate the thread it seems I stumble upon a long known bug. thread.interrupt() does not cancel stream.read().  

There are lots of questions about that. The only thing that should work is to close the underlying stream. But if I close my FileInputStream stream.read() in my receiver thread still keeps waiting. It only stops after receiving the next bunch of data - that of course then goes a wrong way.

What else can I do to really close or shutdown the FileInputStream?

Edit  

After discussing my solution looks like this. It's not best performance and uses available() which is not advised in the docs but it works.

The thread that listens to the file:

```
byte[] buffer = new byte[1024];
while (!isInterrupted())
{
  if (stream.available() == 0)
  {
    Thread.sleep(200);
    continue;
  }

  // now read only executes if there is something to read, it will not block indefinitely.
  // let's hope available() always returns > 0 if there is something to read.
  int size = stream.read(buffer);

  now do whatever you want with buffer, then repeat the while loop
}

```

The sleep duration is a tradeoff between not looping all the time and getting data in an acceptable interval. The Mainthread terminates this with

```
stream.close(); // which has no effect to the thread but is clean
thread.interrupt();

```