Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Premature EOF while reading from Inputstream

tahirakramJan 15 2008 — edited Jan 16 2008
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)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 13 2008
Added on Jan 15 2008
4 comments
4,726 views