Webpage Reading in Java IOException Premature EOF

I often get a Premature EOF exception when reading a web page.

Below is an example of a stacktrace

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.<init>(TalkPage.java:15) at Updater.run(Updater.java:65) 

The following is the getPage () method

 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(); } 

What is an EOFException override and why does this happen in this particular case and how can it be avoided?

Some other information: the page being viewed is about 20 KB, and I read a lot of such pages in my program (about 20,000).

+6
source share
5 answers

This may be due to the fact that you read the content in turn, and for the last line in the file there may be no return to signal the end of the line. Replace your time with the following:

 int BUFFER_SIZE=1024; char[] buffer = new char[BUFFER_SIZE]; // or some other size, int charsRead = 0; while ( (charsRead = rd.read(buffer, 0, BUFFER_SIZE)) != -1) { sb.append(buffer, 0, charsRead); } 
+5
source

This may be due to the server closing the connection. I ran into the same problem when I had a piece of code that opened a connection, performed some other processing and only then tried to load the contents of the input stream - by the time it was in the stream, spending a few seconds on another processing, the server apparently closed the connection, resulting in an IOException: premature EOF. The solution was to always keep track of the contents of the stream - otherwise you leave the HTTP connection open and inactive, and ultimately the server at the other end of the line will hang on you.

+1
source

You can also try setting the buffer size to 1. This helps a little, and if you implement try logic around it, then it should do the trick.

0
source
  StringBuilder sb = new StringBuilder(); try{ URL url = new URL(address); InputStream is = url.openStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader in = new BufferedReader(isr); String str; while((str = in.readLine()) != null){ sb.append(str); sb.append("\n"); } in.close(); isr.close(); is.close(); return sb.toString(); }catch(Exception e){ //OMG.... } 
0
source

All Articles