title: Underlying stream returned zero bytes
body: <p>I'm currently writing my own Brainfuck interpreter in Java and because I want it to be able to read code from files I wrote a <code>BFInputStream</code> that filters unnecessary symbols out. It looks like this:</p>
  <pre><code>import java.io.FilterInputStream;
  import java.io.InputStream;
  import java.io.IOException;
  
  public class BFInputStream extends FilterInputStream {
      public BFInputStream(InputStream in) {
          super(in);
      }
      public int read() throws IOException {
          while(true) {
              int i = super.read();
              // keep LF for line number checking.
              if(i == -1 || i == 10 ||( i &gt;= 43 &amp;&amp; i &lt;= 46) || i == 60 || i == 62 || i == 91 || i == 93)
                  return i;
          }
      }
      public int read(byte[] b, int off, int len) throws IOException {
          if(off &lt; 0 || len &lt; 0 || len &gt; b.length - off) throw new IndexOutOfBoundsException();
          for(int i=0; i&lt;len; i++) {
              int j = read();
              if(j &lt; 1) return i;
              b[off+i] = (byte)j;
          }
          return len;
      }
  }
  </code></pre>
  <p>My Interpreter uses <code>LineNumberReader</code>&lt;-<code>InputStreamReader</code>&lt;-<code>BFInputStream</code>&lt;-<code>FileInputStream</code> to read the file. But everytime it reaches the end of the file it throws:</p>
  <blockquote>
  <p>java.io.IOException: Underlying input stream returned zero bytes</p>
  <pre><code>  at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:268)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
    at java.io.InputStreamReader.read(InputStreamReader.java:167)
    at java.io.BufferedReader.fill(BufferedReader.java:136)
    at java.io.BufferedReader.read(BufferedReader.java:157)
    at java.io.LineNumberReader.read(LineNumberReader.java:108)
    at Interpreter.run(Interpreter.java:101)
    at Interpreter.main(Interpreter.java:180)
  </code></pre>
  </blockquote>
  <p>Interpreter.java:101 contains a call to <code>BFInputStream.read()</code>.</p>
  <p>I'm not sure if I understand the exception correctly. I think the stream returned no bytes at all (because of a timeout). I guess if there was a huge chunk of non-BF characters then that could be possible but at the end of the file? <code>FileInputStream</code> and therefore <code>FilterInputStream</code> and <code>BFInputStream</code> should return -1.</p>
  <p>What is wrong with my code?</p>