tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Ludwig <>
Subject Re: [Http]ServletResponseWrapper.getOutputStream()
Date Wed, 26 Nov 2008 01:14:58 GMT
Martin Gainty schrieb am 22.11.2008 um 12:02:36 (-0500):
> i agree with the unified buffer but i would also make sure all your
> output methods are synchronized

Thanks for your feedback, Martin.

Why exactly would the output methods have to be synchronized? The
request and response objects aren't shared between requests, and a new
instance of a subclass of HttpServletResponseWrapper is created for each
request. And this has no static mutable data. So why synchronized?

> follow the advice on <String/Stream>Writers for incorporating charsets
> and implement encodings

It currently looks like this (in case you want to comment):

package milu;

import javax.servlet.*;
import javax.servlet.http.*;

public class HttpResponseCatcher extends HttpServletResponseWrapper {
 private OutputStream buffer;
 private CapturedServletOutputStream stream;
 private PrintWriter writer;

 // constructor
 public HttpResponseCatcher( HttpServletResponse res) {
  super( res);
  this.buffer = new ByteArrayOutputStream(); = new CapturedServletOutputStream( this.buffer);
  this.writer = new PrintWriter( new OutputStreamWriter(;

 public ServletOutputStream getOutputStream() throws IOException {
  return stream;

 public PrintWriter getWriter() throws IOException {
  return writer;

 public String getCapturedOutput() { return buffer.toString(); }
 public byte[] getByteArray() { return buffer.toString().getBytes(); }
 public char[] getCharArray() { return buffer.toString().toCharArray(); }

Michael Ludwig

To start a new topic, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message