01: /*
02: * Created on May 9, 2006
03: */
04: package uk.org.ponder.servletutil;
05:
06: import java.io.ByteArrayOutputStream;
07: import java.io.OutputStream;
08:
09: import javax.servlet.http.HttpServletResponse;
10:
11: import uk.org.ponder.streamutil.StreamCloseUtil;
12: import uk.org.ponder.util.UniversalRuntimeException;
13:
14: public class ServletResponseWriter {
15: private ByteArrayOutputStream baos;
16: private HttpServletResponse response;
17:
18: private class ServletBAOS extends ByteArrayOutputStream {
19: public void close() {
20: byte[] bytes = baos.toByteArray();
21:
22: OutputStream os = null;
23: try {
24: os = response.getOutputStream();
25: response.setContentLength(bytes.length);
26: response.getOutputStream();
27: os.write(bytes);
28: } catch (Exception e) {
29: throw UniversalRuntimeException.accumulate(e,
30: "Error writing response");
31: } finally {
32: StreamCloseUtil.closeOutputStream(os);
33: }
34: }
35: }
36:
37: public ServletResponseWriter(HttpServletResponse response) {
38: this .response = response;
39: baos = new ServletBAOS();
40: }
41:
42: public OutputStream getOutputStream() {
43: return baos;
44: }
45: }
|