title: java.io.IOException: Premature EOF when reading Transfer-Encoding: chunked http response
body: <p>I've written a small command line program to read web pages and print the response body out to the terminal. </p>
  
  <p>This works well for most sites, however when reading a chunked response from this url: <a href="http://www.pampers.co.uk/home" rel="nofollow">http://www.pampers.co.uk/home</a> I get the following back trace:</p>
  
  <pre><code>java.io.IOException: Premature EOF
  at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:538)
  at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:582)
  at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:669)
  at java.io.FilterInputStream.read(FilterInputStream.java:116)
  at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2668)
  at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
  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.Reader.read(Reader.java:123)
  at HttpPageReader.main(HttpPageReader.java:44)`
  </code></pre>
  
  <p>I've hardcoded the charset to match the response from this page. What steps should I take to debug this issue? I'm assuming this isn't a Java bug but an issue with what's returned by the web server - however I have viewed the page in my web browser and also downloaded it using curl, both without problem.</p>
  
  <p>The code is as follows, should be easy to copy and paste and run stand alone:</p>
  
  <pre><code>import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.URL;
  import java.util.logging.Logger;
  
  public class HttpPageReader
  {    
  private final static Logger logger = Logger.getLogger(HttpPageReader.class.getName());
  
  private static final int SECOND_IN_MILLI_SECONDS = 1000;
  private static final int TIME_OUT_MILLI_SECONDS  = 10 * SECOND_IN_MILLI_SECONDS;
  
  public static void main(String[] args)
  {
      if ( args.length != 1)
      {
          logger.warning("Please provide a url to download");
          System.exit(1);
      }
      logger.info("Downloading url " + args[0] + "...");
  
      try
      {
          URL url = new URL(args[0]);
  
          HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
          httpConn.setUseCaches(false);
          httpConn.setRequestProperty("User-Agent", "My User Agent");
          httpConn.setRequestProperty("Accept", "*/*");
          httpConn.setConnectTimeout(TIME_OUT_MILLI_SECONDS);
          httpConn.setReadTimeout(TIME_OUT_MILLI_SECONDS);
  
          InputStreamReader inputStreamReader = new InputStreamReader(httpConn.getInputStream(), "utf-8");                        
          char chars[] = new char[1000];
          int numRead = inputStreamReader.read(chars);
          StringBuffer stringBuffer = new StringBuffer();
          while (numRead != -1)
          {
              stringBuffer.append(new String(chars, 0, numRead));
              numRead = inputStreamReader.read(chars);
          }
  
          logger.info("done");            
          logger.info(stringBuffer.toString());
  
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }
  
  }   
  }
  </code></pre>
  
  <p>Update: Using IOUtils.toString(httpConn.getInputStream(), "utf-8") as suggested gives pretty much the same backtrace - so the question remains, how do I go about debugging this problem?</p>
  
  <p>Gives:</p>
  
  <pre><code>java.io.IOException: Premature EOF
  at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:538)
  at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:582)
  at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:669)
  at java.io.FilterInputStream.read(FilterInputStream.java:116)
  at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2668)
  at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
  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.Reader.read(Reader.java:123)
  at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1928)
  at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1907)
  at org.apache.commons.io.IOUtils.copy(IOUtils.java:1884)
  at org.apache.commons.io.IOUtils.copy(IOUtils.java:1834)
  at org.apache.commons.io.IOUtils.toString(IOUtils.java:705)
  at org.apache.commons.io.IOUtils.toString(IOUtils.java:730)
  at HttpPageReader.main(HttpPageReader.java:40)
  </code></pre>