ByteArrayOutputStream only gets 1st buffer
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Scott_Oliver
Posted On:   Wednesday, April 17, 2002 11:56 AM

I need to intercept some HTML output generated by a call to a 3rd party appliction. The original call took as a parameter an output stream object like this: ...... renderMenuBar(new PrintWriter(out), ..... As a solution to a previous problem (where I just wanted to make the output disappear), someone suggested replacing the "new PrintWriter(out)" parameter with one providing a ByteArrayOutputStream object, like this: ByteArrayOutputStream bitBucket = new java.io.ByteArrayOutputStream(10000); ...... renderMenuBar(new PrintWriter(bitBucket ), ..... That worked great, and I believed it would solve my current problem as well; I could just use bitBucket.toByteArray   More>>

I need to intercept some HTML output generated by a call to a 3rd party appliction. The original call took as a parameter an output stream object like this:

...... renderMenuBar(new PrintWriter(out), .....




As a solution to a previous problem (where I just wanted to make the output disappear), someone suggested replacing the "new PrintWriter(out)" parameter with one providing a
ByteArrayOutputStream object, like this:

			ByteArrayOutputStream bitBucket = new java.io.ByteArrayOutputStream(10000);
			

...... renderMenuBar(new PrintWriter(bitBucket ), .....




That worked great, and I believed it would solve my current problem as well; I could just use bitBucket.toByteArray() or bitBucket.toString() and manipulate the HTML to my hearts content. Unfortunately, no.



That plan works fine except for one small problem: I'm only seeing the first 8k of the output (bitBucket.size() returns 8192).



This is in a JSP environment, so one of the first things I tried to do was change the buffer size through the JSP "page buffer=20kb" directive but that didn't help. I also tried calling the toByteArray() method twice to see if it would grab the next buffer, but that just gave me the same output. I've tried allocating the ByteArrayOutputStream object with the default size and let it grow and with a specific size (10k); in both cases all I get are 8192 bytes.



How can I retrieve the subsequent buffers?



This is a rather crucial problem for me to resolve, so any advice or suggestions would be most appreciated!



-Scott    <<Less

Re: ByteArrayOutputStream only gets 1st buffer

Posted By:   Daniel_Grieves  
Posted On:   Friday, April 19, 2002 07:50 AM

I'm not sure what renderMenuBar does, but it sounds to me like the problem is not in your method of capturing the data but instead in the piece of code that writes to this ByteArrayOutputStream.

What I mean is this. A ByteArrayOutputStream is designed to work like a Vector - it grows the size of the array as needed to hold all the data it receives. So the problem is not that the ByteArrayOutputStream is not holding all the data but that the renderMenuBar() method is not writing all the data to the stream. Perhaps it is buffering, and the buffer is not getting flushed?

Dan

Re: ByteArrayOutputStream only gets 1st buffer

Posted By:   Stephen_Ostermiller  
Posted On:   Thursday, April 18, 2002 04:29 AM

Use a PipedOutputStream to pass to this method and connect it to a PipedInputStream from which you can read. Alternativly, you can get CircularByteBuffer code which makes this process much easier, just create a circular buffer, pass the OutputStream from it to the producer and consume the input from the InputStream.

http://ostermiller.org/utils/CircularByteBuffer.html
About | Sitemap | Contact