• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Premature EOF while reading from Inputstream

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Everybody's invited. Except this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic