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.
If server is causing this problem
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?
Originally posted by Tahir Akram: 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. [/CODE]
Firstly, the reason why BufferedReader is not keeping up to the contract of readLine(). As you can see from the stack trace, ChunkedInputStream.readAheadBlocking is throwing an EOFException and this makes HttpURLConnection$HttpInputStream.read break the contract of read( i.e. read should return -1 when EOF is reached.) Since, the buffered reader relies on the contract of read to abide by the contract of readline(), readline also fails.
I think the straight forward solution will be, not to read using BufferedReader but one byte at a time and put the read in a try-catch for EOFException.
Other way is to put a mark at the position where the last line ended i.e. put a mark right after readLine() returns. In case you get an EOFException read one byte at a time from the last mark. (This sounds like a feasible solution, i have not tried though.) NOTE: Marking will require a readaheadlimit, providing a value for which will be a little difficult as it depends on the max bytes in a line.