Question

My apache HTTP client is reading data from a server which is sending HTTP streaming (in form of chunked data) to us.

I have a input stream and Buffered Reader to get data, and reading this data using Buffered Data readLine method.

InputStream inputStream = httpEntity.getContent();
br= new BufferedReader(new InputStreamReader(inputStream));

while ((line = br.readLine()) != null) 

I want to close the connection if I get a wrong response from server.

For this I am creating a timertask at a time interval to close my connection. I am parsing the response each time I receive it to check if the response is correct, if the response is correct then I reset the timetask. If I find a wrong response in that case I don't reset the timertask (and let the timertask be executed to the time it was previously set for)

I want to know if using unbuffered I/O can help? or I need some other approach?

Problem I see is since the connection is open, my timertask is unable to close the connection. inputStream.close or bufferReader.close both are not able to execute (seem to infinitely wait)

Was it helpful?

Solution

BufferReader.close() will only close the inputStream, which in turn will call inputStream.close(). According to the java api InputStream.close() does nothing. You need to close the socket, or whatever it is that you are abstracting by httpEntity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top