tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Ludwig <mil...@gmx.de>
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
> http://java.sun.com/j2se/1.4.2/docs/api/java/io/OutputStreamWriter.html

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

package milu;

import java.io.*;
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();
  this.stream = new CapturedServletOutputStream( this.buffer);
  this.writer = new PrintWriter( new OutputStreamWriter( this.stream));
 }

 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: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message