001    package echopoint.util.io;
002    
003    /*
004     * This file is part of the Echo Point Project.  This project is a collection
005     * of Components that have extended the Echo Web Application Framework.
006     *
007     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
008     *
009     * The contents of this file are subject to the Mozilla Public License Version
010     * 1.1 (the "License"); you may not use this file except in compliance with
011     * the License. You may obtain a copy of the License at
012     * http://www.mozilla.org/MPL/
013     *
014     * Software distributed under the License is distributed on an "AS IS" basis,
015     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
016     * for the specific language governing rights and limitations under the
017     * License.
018     *
019     * Alternatively, the contents of this file may be used under the terms of
020     * either the GNU General Public License Version 2 or later (the "GPL"), or
021     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
022     * in which case the provisions of the GPL or the LGPL are applicable instead
023     * of those above. If you wish to allow use of your version of this file only
024     * under the terms of either the GPL or the LGPL, and not to allow others to
025     * use your version of this file under the terms of the MPL, indicate your
026     * decision by deleting the provisions above and replace them with the notice
027     * and other provisions required by the GPL or the LGPL. If you do not delete
028     * the provisions above, a recipient may use your version of this file under
029     * the terms of any one of the MPL, the GPL or the LGPL.
030     */
031    
032    import java.io.ByteArrayOutputStream;
033    import java.io.IOException;
034    import java.io.PrintWriter;
035    import java.io.StringWriter;
036    import java.io.UnsupportedEncodingException;
037    
038    /**
039     * An PrintWriter that will capture the content as it otherwise passes into the
040     * ether.
041     */
042    public class CapturedPrintWriter extends PrintWriter {
043    
044            private ByteArrayOutputStream out;
045    
046            private String characterEncoding;
047    
048            public CapturedPrintWriter(ByteArrayOutputStream out, String characterEncoding) {
049                    super(new StringWriter());
050                    this.out = out;
051                    this.characterEncoding = characterEncoding;
052            }
053    
054            /**
055             * @see java.io.PrintWriter#checkError()
056             */
057            public boolean checkError() {
058                    return false;
059            }
060    
061            /**
062             * @see java.io.Writer#close()
063             */
064            public void close() {
065                    try {
066                            out.close();
067                    } catch (IOException e) {
068                    }
069            }
070    
071            /**
072             * @see java.io.Writer#flush()
073             */
074            public void flush() {
075                    try {
076                            out.flush();
077                    } catch (IOException e) {
078                    }
079            }
080    
081            /**
082             * @see java.io.PrintWriter#print(boolean)
083             */
084            public void print(boolean b) {
085                    print(String.valueOf(b));
086            }
087    
088            /**
089             * @see java.io.PrintWriter#print(char)
090             */
091            public void print(char c) {
092                    write(c);
093            }
094    
095            /**
096             * @see java.io.PrintWriter#print(char[])
097             */
098            public void print(char[] arr) {
099                    write(arr);
100            }
101    
102            /**
103             * @see java.io.PrintWriter#print(double)
104             */
105            public void print(double d) {
106                    print(String.valueOf(d));
107            }
108    
109            /**
110             * @see java.io.PrintWriter#print(float)
111             */
112            public void print(float f) {
113                    print(String.valueOf(f));
114            }
115    
116            /**
117             * @see java.io.PrintWriter#print(int)
118             */
119            public void print(int i) {
120                    print(String.valueOf(i));
121            }
122    
123            /**
124             * @see java.io.PrintWriter#print(long)
125             */
126            public void print(long l) {
127                    print(String.valueOf(l));
128            }
129    
130            /**
131             * @see java.io.PrintWriter#print(java.lang.Object)
132             */
133            public void print(Object obj) {
134                    print(String.valueOf(obj));
135            }
136    
137            /**
138             * @see java.io.PrintWriter#print(java.lang.String)
139             */
140            public void print(String s) {
141                    writeBytes(toEncodedBytes(s));
142            }
143    
144            /**
145             * @see java.io.PrintWriter#println()
146             */
147            public void println() {
148                    write('\n');
149            }
150    
151            /**
152             * @see java.io.PrintWriter#println(boolean)
153             */
154            public void println(boolean x) {
155                    println(String.valueOf(x));
156            }
157    
158            /**
159             * @see java.io.PrintWriter#println(char)
160             */
161            public void println(char x) {
162                    println(String.valueOf(x));
163            }
164    
165            /**
166             * @see java.io.PrintWriter#println(char[])
167             */
168            public void println(char[] x) {
169                    write(x);
170                    write('\n');
171            }
172    
173            /**
174             * @see java.io.PrintWriter#println(double)
175             */
176            public void println(double x) {
177                    println(String.valueOf(x));
178            }
179    
180            /**
181             * @see java.io.PrintWriter#println(float)
182             */
183            public void println(float x) {
184                    println(String.valueOf(x));
185            }
186    
187            /**
188             * @see java.io.PrintWriter#println(int)
189             */
190            public void println(int x) {
191                    println(String.valueOf(x));
192            }
193    
194            /**
195             * @see java.io.PrintWriter#println(long)
196             */
197            public void println(long x) {
198                    println(String.valueOf(x));
199            }
200    
201            /**
202             * @see java.io.PrintWriter#println(java.lang.Object)
203             */
204            public void println(Object x) {
205                    println(String.valueOf(x));
206            }
207    
208            /**
209             * @see java.io.PrintWriter#println(java.lang.String)
210             */
211            public void println(String x) {
212                    println(x.toCharArray());
213            }
214    
215            /**
216             * @see java.io.PrintWriter#setError()
217             */
218            protected void setError() {
219                    ; // do nothing
220            }
221    
222            /**
223             * @see java.io.Writer#write(char[], int, int)
224             */
225            public void write(char[] buf, int off, int len) {
226                    for (int i = off; i < len; i++) {
227                            write(buf[i]);
228                    }
229            }
230    
231            /**
232             * @see java.io.Writer#write(char[])
233             */
234            public void write(char[] buf) {
235                    write(buf, 0, buf.length);
236            }
237    
238            /**
239             * @see java.io.Writer#write(int)
240             */
241            public void write(int c) {
242                    out.write(c);
243            }
244    
245            /**
246             * @see java.io.Writer#write(java.lang.String, int, int)
247             */
248            public void write(String s, int off, int len) {
249                    writeBytes(toEncodedBytes(s), off, len);
250            }
251    
252            /**
253             * @see java.io.Writer#write(java.lang.String)
254             */
255            public void write(String s) {
256                    writeBytes(toEncodedBytes(s));
257            }
258    
259            private void writeBytes(byte[] buf) {
260                    writeBytes(buf,0,buf.length);
261            }
262    
263            private void writeBytes(byte[] buf, int off, int len) {
264                    for (int i = 0; i < len; i++) {
265                            write((int) buf[i + off]);
266                    }
267            }
268    
269            private byte[] toEncodedBytes(String s) {
270                    if (this.characterEncoding != null) {
271                            try {
272                                    return s.getBytes(this.characterEncoding);
273                            } catch (UnsupportedEncodingException e) {
274                                    return s.getBytes();
275                            }
276                    } else {
277                            return s.getBytes();
278                    }
279            }
280    
281    }