• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

consuming/finishing a stream

 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going by the recommendations here:
http://jakarta.apache.org/commons/httpclient/3.0/performance.html

I'm using the Apache Commons HttpClient to load test our website, and I don't want the test program to go onto the next HTTP method until it has read all the bytes from the response to each request. Here's my code:



I'm trying to figure out if the following bit fully reads the stream of bytes from the web server, or if you have to start calling read methods to get it to do that:



If the above bit does it, then I guess I don't need the BufferedReader part...I'm going to be running hundreds of thousands of GET and POST methods, so I want to make sure that it really is waiting to read all the bytes each time, but I don't want to go so far as to write out all these bytes to file or anything like that...
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to stick to using streams so you can avoid all the unnecessary character encoding. DataInputStream's readFully(byte[]) method (from DataInput) would be very helpful here.Of course, InputStream.read(byte[]) would be just as useful.
[ February 10, 2005: Message edited by: David Harkness ]
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you do have to make sure that the buffer is large enough to read the whole thing, right?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the buffer's size only affects how much will be read in any given call to read(). If it fills the buffer, it will return the size of the buffer as read() returns the number of bytes read. The loop condition passes (BUFFER_SIZE != -1) and read() is called again to refill the buffer. The call to read() returns -1 only when the stream's end is reached (server closes the socket in your case).

You can reuse and share the buffer because you don't care about any of the bytes that you read -- only that you've read them from the stream. The threads will keep overwriting each others bytes, but since a byte array has no concept of "empty" or "full" it won't matter.
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, duh...it's in a while loop...my brain wasn't on track when I read your post! What a day...

I could use some and

Or maybe I just need some


...
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephen Huey:
I could use some and
I hear ya. Do those first, then get sleep to recover.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic