• Post Reply Bookmark Topic Watch Topic
  • New Topic

Premature EOF while reading from Inputstream

 
Tahir Akram
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

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?

any help will be really appreciated.




exception trace
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Solution:

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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!