Christopher Schultz schrieb am 01.12.2008 um 13:15:24 (-0500):
> Michael Ludwig wrote:

[Better late than never.]

> >  public HttpResponseCatcher( HttpServletResponse res) {
> >   super( res);
> >   this.buffer = new ByteArrayOutputStream();
> >   this.stream = new CapturedServletOutputStream( this.buffer);
> >   this.writer = new PrintWriter( new OutputStreamWriter( this.stream));
> 
> ! No character encoding!

Yes, the ctor OutputStreamWriter(OutputStream out, String charsetName)
should be used, so:

  new OutputStreamWriter( this.stream, this.getCharacterEncoding())

> I would lazily instantiate these objects -- at least the stream and
> writer. There's no need to create a writer if the caller doesn't ask
> for one.

Okay, probably like this:

  private ByteArrayOutputStream buffer;
  private CapturedServletOutputStream stream;

  private StringWriter stringWriter;
  private PrintWriter writer;

  public ServletOutputStream getOutputStream() throws IOException {
    getResponse().getOutputStream();
    if ( buffer == null) {
      buffer = new ByteArrayOutputStream();
      stream = new CapturedServletOutputStream( buffer);
    }
    return stream;
  }

  public PrintWriter getWriter() throws IOException {
    getResponse().getWriter();
    if ( stringWriter == null ) {
      stringWriter = new StringWriter();
      writer = new PrintWriter( stringWriter);
    }
    return writer;
  }

I'm using a StringWriter as a backing class here, following a
recommendation you made in a previous post (20.11.08).

> Also, the caller might want to set the character encoding on the
> response, and you have to allow that to occur before you create your
> writer.

Now has to be handled by the underlying response object.

> >  public byte[] getByteArray() { return buffer.toString().getBytes(); }
> 
> Yikes! You aren't checking the character encoding of the response! You
> definitely need to do this in order to properly implement this method.

Yes, this should be:

    return buffer.toString( this.getCharacterEncoding()).getBytes()

And "buffer" in this case has to be of type ByteArrayOutputStream, not
just OutputStream.

Thanks for all your help,

Michael Ludwig

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to