KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > filter > CharArrayResponseWrapper


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.servlet.filter;
4
5 import java.io.PrintWriter JavaDoc;
6
7 import javax.servlet.ServletOutputStream JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
10
11 import jodd.util.CharUtil;
12 import jodd.io.FastCharArrayWriter;
13
14 /**
15  * A response wrapper that takes everything the client would normally output
16  * and saves it in one big character array.
17  */

18 public class CharArrayResponseWrapper extends HttpServletResponseWrapper JavaDoc {
19
20     private FastCharArrayWriter writer;
21
22     /**
23      * Initializes wrapper.
24
25      * First, this constructor calls the parent constructor. That call is crucial
26      * so that the response is stored and thus setHeader, setStatus, addCookie,
27      * and so forth work normally.
28
29      * Second, this constructor creates a CharArrayWriter that will be used to
30      * accumulate the response.
31      */

32     public CharArrayResponseWrapper(HttpServletResponse JavaDoc response) {
33         super(response);
34         writer = new FastCharArrayWriter();
35     }
36
37     /**
38      * When servlets or JSP pages ask for the Writer, don't give them the real
39      * one. Instead, give them a version that writes into the character array.
40      * The filter needs to send the contents of the array to the client (perhaps
41      * after modifying it).
42      */

43     public PrintWriter JavaDoc getWriter() {
44         return new PrintWriter JavaDoc(writer);
45     }
46
47     /**
48      * Get a String representation of the entire buffer.
49      *
50      * Be sure <b>not</b> to call this method multiple times on the same wrapper.
51      * The API for CharArrayWriter does not guarantee that it "remembers" the
52      * previous value, so the call is likely to make a new String every time.
53      */

54     public String JavaDoc toString() {
55         return writer.toString();
56     }
57
58     /**
59      * Get the underlying character array.
60      */

61     public char[] toCharArray() {
62         return writer.toCharArray();
63     }
64
65     /**
66      * Get the underlying as byte array.
67      */

68     public byte[] toByteArray() {
69         return CharUtil.toByteArray(writer.toCharArray());
70     }
71
72
73     /**
74      * This empty method <b>must</b> exist.
75      */

76     public void setContentLength(int len) {
77         super.setContentLength(len);
78     }
79
80     /**
81      * Returns the size (number of characters) of written data.
82      */

83     public int getSize() {
84         return writer.size();
85     }
86
87
88     private String JavaDoc contentType = "";
89
90     /**
91      * Sets the content type.
92      */

93     public void setContentType(String JavaDoc type) {
94         super.setContentType(type);
95         contentType = type;
96     }
97
98     /**
99      * Returns content type.
100      */

101     public String JavaDoc getContentType() {
102         return contentType;
103     }
104
105     public void close() {
106         writer.close();
107     }
108
109     public void reset() {
110         writer.reset();
111     }
112
113     /**
114      * Returns output stream.
115      */

116     public ServletOutputStream JavaDoc getOutputStream() {
117
118         return new ServletOutputStream JavaDoc() {
119             public void write(int b) {
120                 writer.write(b);
121             }
122
123             public void write(byte b[]) {
124                 writer.write(CharUtil.toCharArray(b), 0, b.length);
125             }
126
127             public void write(byte b[], int off, int len) {
128                 writer.write(CharUtil.toCharArray(b), off, len);
129             }
130         };
131     }
132
133 }
134
Popular Tags