title: Reading a web page in Java IOException Premature EOF
body: <p>I am frequently getting a 'Premature EOF' Exception when reading a web page.</p>
  
  <p>The following is the StackTrace</p>
  
  <pre><code>java.io.IOException: Premature EOF
      at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:556)
      at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:600)
      at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:687)
      at java.io.FilterInputStream.read(FilterInputStream.java:133)
      at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2968)
      at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
      at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
      at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
      at java.io.InputStreamReader.read(InputStreamReader.java:184)
      at java.io.BufferedReader.fill(BufferedReader.java:154)
      at java.io.BufferedReader.readLine(BufferedReader.java:317)
      at java.io.BufferedReader.readLine(BufferedReader.java:382)
      at Utilities.getPage(Utilities.java:24)  while ((line = rd.readLine()) != null) {
      at TalkPage.&lt;init&gt;(TalkPage.java:15)
      at Updater.run(Updater.java:65)
  </code></pre>
  
  <p>Following is the getPage() method</p>
  
  <pre><code>public static String getPage(String urlString) throws Exception {
      URL url = new URL(urlString);
      URLConnection conn = url.openConnection();
      BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      StringBuffer sb = new StringBuffer();
      String line;
      while ((line = rd.readLine()) != null) {  // LINE 24
          sb.append(line);
      }
      return sb.toString();
  }
  </code></pre>
  
  <p>What is a permature EOFException and why is it occuring in this particular case and how can it be avoided?</p>
  
  <p>Some other information: The size of the page being read is around 20 KB and I'm reading many such pages in my program ( around 20 000 )</p>