This discussion is archived
4 Replies Latest reply: Jan 15, 2008 10:43 PM by 807603 RSS

Premature EOF while reading from Inputstream

800317 Newbie
Currently Being Moderated
Hello all;

I am hitting on a HTTPs URL and got the response but while reading from BufferReader I got the Premature EOF exception. This exception occur when buffer reader contain last record. Instead of after reading last line, it should attempt to read next line which will be EOF, but while reading last one this exception occur.

I am reading csv contents from server. I have used this code before for posting and getting XML response from server.

I also found the same problem with a guy that this exception occur while reading the last line.

Please help me what is getting wrong with my code and suggest some pointers.

code
URL url = null;
HttpsURLConnection httpsCon = null;
try{
     url = new URL("https://<some_url>");
     
     httpsCon = (HttpsURLConnection)url.openConnection();

     httpsCon.setRequestMethod(requestMethod);
     httpsCon.setDoOutput(true);
     httpsCon.setRequestProperty("Content-Type", contentType);
     httpsCon.setRequestProperty("Content-Length", String.valueOf(dataLength));
     
     wr = new OutputStreamWriter(httpsCon.getOutputStream());
     wr.write(data);
     wr.flush();
               
     InputStreamReader isr = new InputStreamReader(httpsCon.getInputStream());
               
     BufferedReader rd = new BufferedReader(isr);
     String line;
     StringBuffer respBuff = new StringBuffer();
                    
     /* when rd.readLine contain last line of the file, it throws exception;
 instead of going in the loop */
        while ((line = rd.readLine()) != null) {
          respBuff.append(line);
     }          
}catch(Exception e){
     e.printStackTrace();
}
Exception trace:

ava.io.IOException: Premature EOF
     at sun.net.www.http.ChunkedInputStream.readAheadBlocking(Unknown Source)
     at sun.net.www.http.ChunkedInputStream.readAhead(Unknown Source)
     at sun.net.www.http.ChunkedInputStream.read(Unknown Source)
     at java.io.FilterInputStream.read(Unknown Source)
     at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
     at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)
     at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source)
     at sun.nio.cs.StreamDecoder.read(Unknown Source)
        at sun.nio.cs.StreamDecoder.read0(Unknown Source)
     at sun.nio.cs.StreamDecoder.read(Unknown Source)
     at java.io.InputStreamReader.read(Unknown Source)
  • 1. Re: Premature EOF while reading from Inputstream
    800317 Newbie
    Currently Being Moderated
    Any idea about the problem?
  • 2. Re: Premature EOF while reading from Inputstream
    807603 Newbie
    Currently Being Moderated
    The server is causing this error and there is really nothing you can do about it.
  • 3. Re: Premature EOF while reading from Inputstream
    800317 Newbie
    Currently Being Moderated
    Thanks cotton for your reply.

    Is this can be possibilities:

    stream terminated before I reached EOF,
    |---------------------- no EOF marker

    or EOF maker comes earlier before it should (at the end).
    |----------------EOF-----|

    (talking about 2nd case) If I use readInt of inputstream and check the decimal value of EOF (which I think 26) then I can prevent occurring exception?

    any help will be really appreciated.
  • 4. Re: Premature EOF while reading from Inputstream
    807603 Newbie
    Currently Being Moderated
    tahirakram wrote:
    then I can prevent occurring exception?
    I don't believe there is anything you can do to prevent the exception since it is caused by a server error.

    You might want to consider storing the content in some sort of buffer and deciding if and when the exception occurs how to proceed. For example if you are just missing the last line then just basically ignore the exception and continue. I don't know what else to tell you.

    After looking into the matter it is my understanding that the problem is likely that the server has mangled the chunks it is sending you and that the last chunk sent before the socket is closed is smaller than had been previously promised. This in turn causes the input stream to freak out.

    But again nothing that YOU can do about that. The server needs to be fixed, or as mentioned above you decide to catch the exception in question and proceed accordingly.

    You may want to consider reposting this question into the Networking forum to see if the expert there can give you more information.