ByteArrayOutputStream.size() to small?

I

Ido M. Tamir

Hi,

I am writing a ByteArrayOutputStream to a ServletOutputStream.
But my webserver tells me that i have specified an invalid length
as my contentLength.
It should be 2x + ~11 bytes longer. How could I get the correct size?


HttpServletResponse response = getResponse();
ByteArrayOutputStream bout = createStream();
response.setHeader("Content-disposition", "attachment; filename="
+ "myfile.ps");
response.setContentType("application/postscript");
response.setContentLength(bout.size() );
ServletOutputStream sout = response.getOutputStream();
bout.writeTo(sout);
bout.close();
response.flushBuffer();
 
J

John C. Bollinger

Ido said:
Hi,

I am writing a ByteArrayOutputStream to a ServletOutputStream.
But my webserver tells me that i have specified an invalid length
as my contentLength.
It should be 2x + ~11 bytes longer. How could I get the correct size?


HttpServletResponse response = getResponse();
ByteArrayOutputStream bout = createStream();
response.setHeader("Content-disposition", "attachment; filename="
+ "myfile.ps");
response.setContentType("application/postscript");
response.setContentLength(bout.size() );
ServletOutputStream sout = response.getOutputStream();
bout.writeTo(sout);
bout.close();
response.flushBuffer();

The code you show should get the correct size, as of the time at which
bout.size() is invoked. If, however, there is any chance that some
other thread is writing to the stream, then by the time you attempt to
dump it to the servlet's output stream it may be longer than it was when
you set the size on the response. If that doesn't explain it then I
would need to see a working example, as minimal as possible, that
exhibits the problem. Chances are that the bug is not in the part of
the code you showed.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top