title: Strange behavior of EOFException
body: <p>I have some simple class that is <code>DataInputStream</code> stream to read from file.</p>
  
  <p>I have surrounded this stream with <code>EOFException</code> try-catch block.</p>
  
  <p>It has some strange behavior coz sometimes it throws EOFException into text that is read.</p>
  
  <p>Output to console:</p>
  
  <pre><code>"The vessel was in as good condition as I am, and as, I hope
  you ar#End of streame#, M. Morrel, and this day and a half was lost from
  pure whim, for the pleasure of going ashore, and nothing
  else."
  </code></pre>
  
  <p>I couldn't figure out what is cause of this strange behavior...</p>
  
  <p>Here is code snippet:</p>
  
  <pre><code>public class FormattedMemoryInput {    
      public static void main(String[] args) throws IOException {
          boolean done = false;    
          try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(
                  BufferedInputFile.read("./gutenberg/cristo.txt").getBytes()));) {
  
              while (!done) {
                  System.out.print((char) in.readByte());
              }
          } catch (EOFException e) {
              System.err.println("#End of stream#");
          }
      }
  } 
  </code></pre>
  
  <p>It uses static method <code>BufferedInputFile.read()</code> to read first 500 lines:</p>
  
  <pre><code>public class BufferedInputFile {
  
      // Throw exceptions to console:
      public static String read(String filename) throws IOException {
  
          // Reading input by lines:
          BufferedReader in = new BufferedReader(new FileReader(filename));
          StringBuilder sb = new StringBuilder();
          String s;
          int i = 0;
  
          while ((s = in.readLine()) != null &amp;&amp; (i &lt; 500)) {
              sb.append(s + "\n");
              i++;
          }
  
          in.close();
          return sb.toString();
      }
  </code></pre>
  
  <ul>
  <li><em><strong>Why EOFException is thrown into text?</em></strong></li>
  </ul>
  
  <p><strong>Solution:</strong></p>
  
  <p>It was at adding one line:</p>
  
  <pre><code>while (!done) {
      System.out.print((char) in.readByte());
      System.out.flush(); // this one
  }
  </code></pre>