Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedInputStream(Very Uregent)  RSS feed

 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I getting whole messagebody message(which contains header, footer part of HTTP and image content) from browser to java application using HTTP post method. Now somehow, sometimes,using BufferedInputStream doesn't read all bytes data from messagebody(for example: If content length is 10643, but stream only reads 8192) Why???
Thanks,
Angela
 
Joe Joe
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BufferedInputStream in = new BufferedInputStream(something);
byte mybyte = new byte[in.available()];
in.read(xx);
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Joe:
BufferedInputStream in = new BufferedInputStream(something);
byte mybyte = new byte[in.available()];
in.read(xx);

I tried :
BufferInputStream in = new BufferedInputStream(socket.getInputStream());
byte b[] = new byte[in.available()];
int len = in.read(b);
I did system.out.println(len), gives me 0
Thanks,
Angela
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How big was b.length? I bet it's 0.
You can't rely on available to tell you how many bytes are going to be read from the stream. It only tells how many bytes "can be read without blocking".
The base InputStream.vvailable() always returns 0 (that is, it always has to block to get the next byte).
BufferedInputStream.available() reports how many bytes are in its buffer (bytes that it has buffered but you have not yet read). If you call available before you have read anything, the buffer is probably empty so it should return 0.
One reason you can not read the entire stream in one call may be that the other end of the stream (the server) has not have finished writing the whole stream. Waiting before you read may not help either because the server may block if the stream/pipe buffer fills up.
So you have to keep reading until you get to the end of the stream.
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Landers:

So you have to keep reading until you get to the end of the stream.

I tried with while loop until it reach to end, but somehow when I tried while loop, doesn't come out from while loop.
Thanks,
Angela
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The read(byte[]) method will return -1 when the end of stream is reached. Make sure your while loop is testing for that.
Usually, it's something like:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!