ByteArrayOutputStream.size() to small?

Discussion in 'Java' started by Ido M. Tamir, Jun 24, 2004.

  1. Ido M. Tamir

    Ido M. Tamir Guest

    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();
    Ido M. Tamir, Jun 24, 2004
    #1
    1. Advertising

  2. Ido M. Tamir wrote:

    > 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
    John C. Bollinger, Jun 24, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    454
    =?Utf-8?B?Qg==?=
    Nov 17, 2005
  2. Peter Diedrich

    Drop down form element with small size

    Peter Diedrich, Sep 5, 2003, in forum: HTML
    Replies:
    2
    Views:
    525
    Peter Diedrich
    Sep 10, 2003
  3. Angus
    Replies:
    3
    Views:
    1,283
    Victor Bazarov
    Jan 3, 2007
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,504
    Michael Jung
    May 25, 2008
  5. Googy
    Replies:
    3
    Views:
    224
    James Edward Gray II
    Jun 29, 2007
Loading...

Share This Page