This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

BufferedReader is hanging while reading  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using BufferedReader for reading a response from the server.

while((String line = br.readLine()) != null)
System.out.println(line);

Above is my code. When I run I am getting only few lines but not all the lines.

If br completes reading a line then it should return null and come out of the loop. That is also not happening. It is just waiting for the line to read.

Any idea why this is happening ?
 
Marshal
Posts: 61727
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sai Vaishnavi wrote:. . . If br completes reading a line then it should return null and come out of the loop. . . .

No, it doesn't. The line won't be null until the server signals it is null. The reader will block while it waits for the next line. That is different from the program hanging. You would have to provide lots more details before we can help you any more, particularly about your getting fewer lines than you expected.
 
Sai Vaishnavi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My response is like :

<xml version="1.0"?>
<reply>
<firstname>xxxxxx</firstname>
<amount>xxxxxx<?amount>
<year>xxxxxx</year>
</reply>

In the output I'm getting only the first line which is xml version and br is waiting for the next line to read.
 
Sai Vaishnavi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Sai Vaishnavi wrote:. . . No, it doesn't. The line won't be null until the server signals it is null. The reader will block while it waits for the next line. That is different from the program hanging



Thanks for clarifying.

 
Saloon Keeper
Posts: 2150
278
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which protocol is being used to transport this payload?

With HTTP, there would often be a Content-Length header (unless chunked transfer encoding is being used) which indicates the number of bytes to expect.  For example:
BTW: the XML you posted is mal-formed
 
Sai Vaishnavi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see any content length in the response. After hitting the req I'm getting the response that starts with XML tag
 
Ron McLeod
Saloon Keeper
Posts: 2150
278
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sai Vaishnavi wrote:I can't see any content length in the response.



Ron McLeod wrote:Which protocol is being used to transport this payload?


If you are using HTTP, did you check the HTTP headers?
 
Bartender
Posts: 19976
95
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Briefly, a BufferedReader will hang when a demand is made for data but the input buffer is empty and the BufferedReader has not either reached an end-of-data indicator (where applicable) not been closed.

As an example, you can process data coming from a disk file MUCH faster than you can read it from the disk drive. So the buffer gets sucked dry and you're blocked until the disk can load the buffer up again. It gets even more obvious when the data is coming from a high-latency network source, and it's especially obvious when you are getting your data from a terminal device and you have to wait while the drooling user gets done typing. That particular case can also suffer if you don't have your input source set up to recognize a carriage return or linefeed as a "go ahead" indicator. If you have a 4K buffer and the terminal isn't configured to mark the buffer as "filled" when you hit Return, then typing "Hello<CR>" won't do you any good - it's going to wait until you've typed 4K characters before it unblocks.
 
Rancher
Posts: 3317
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are all the lines properly terminated?  The API doc for the readLine method says:


Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.


The readLine can hang waiting for an end of line
 
Sai Vaishnavi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:
If you are using HTTP, did you check the HTTP headers?



Yea I checked for the http headers and content length is not zero,  it is above 300  
 
Norm Radder
Rancher
Posts: 3317
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try a little debug code to see what is being read:

Note: I get a compiler error with the code from the first post:
 
Campbell Ritchie
Marshal
Posts: 61727
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that because you are declaring the String inside the ()
 
Norm Radder
Rancher
Posts: 3317
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Above is my code. When I run I am getting only few lines but not all the lines.


As The OP said.  
I don't think the posted code was copied from his program because it does not compile.  Makes you wonder what else is different if the code was just typed in from memory.
 
Sai Vaishnavi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Try a little debug code to see what is being read:



Thanks, this helped me in reading the whole response. I have two questions here.
1.After reading the whole response, the loop is not terminating. why ?
2.What is the difference between read and readline. As I am able to read using br.read() but not with br.readline()
 
Sai Vaishnavi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Is that because you are declaring the String inside the ()



I tried by declaring outside of the loop and I'm getting the same output (reading only first line).
 
Norm Radder
Rancher
Posts: 3317
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks, this helped me in reading the whole response.


Did the message end with a newline character?  The readLine method needs that to finish.  See my earlier post.

the loop is not terminating.

The connection is still open and the code is waiting for more data to read.  Close the connection on the sending side to end it.

difference between read and readline

Read the API doc for those methods.
read reads one char
readLine reads characters up to a newline character
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!