File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes consuming/finishing a stream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "consuming/finishing a stream" Watch "consuming/finishing a stream" New topic
Author

consuming/finishing a stream

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Jul 15, 2003
Posts: 618
But you do have to make sure that the buffer is large enough to read the whole thing, right?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Jul 15, 2003
Posts: 618
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

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Stephen Huey:
I could use some and
I hear ya. Do those first, then get sleep to recover.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: consuming/finishing a stream
 
Similar Threads
java io question, conceptual query
confused with java.io.
IO Question from javacaps
system.in.read()
IO Question