001    package echopoint.util.io;
002    /*
003     * This file is part of the Echo Point Project.  This project is a collection
004     * of Components that have extended the Echo Web Application Framework.
005     *
006     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
007     *
008     * The contents of this file are subject to the Mozilla Public License Version
009     * 1.1 (the "License"); you may not use this file except in compliance with
010     * the License. You may obtain a copy of the License at
011     * http://www.mozilla.org/MPL/
012     *
013     * Software distributed under the License is distributed on an "AS IS" basis,
014     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015     * for the specific language governing rights and limitations under the
016     * License.
017     *
018     * Alternatively, the contents of this file may be used under the terms of
019     * either the GNU General Public License Version 2 or later (the "GPL"), or
020     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
021     * in which case the provisions of the GPL or the LGPL are applicable instead
022     * of those above. If you wish to allow use of your version of this file only
023     * under the terms of either the GPL or the LGPL, and not to allow others to
024     * use your version of this file under the terms of the MPL, indicate your
025     * decision by deleting the provisions above and replace them with the notice
026     * and other provisions required by the GPL or the LGPL. If you do not delete
027     * the provisions above, a recipient may use your version of this file under
028     * the terms of any one of the MPL, the GPL or the LGPL.
029     */
030    import java.io.ByteArrayOutputStream;
031    import java.io.IOException;
032    
033    import javax.servlet.ServletOutputStream;
034    
035    /**
036     * An ServletOutputStream that will capture the content
037     * as it otherwise passes into the ether.
038     */
039    public class CapturedServletOutputStream extends ServletOutputStream {
040    
041            private ByteArrayOutputStream out;
042    
043            public CapturedServletOutputStream(ByteArrayOutputStream out) {
044                    this.out = out;
045            }
046    
047            /**
048             * @see java.io.OutputStream#close()
049             */
050            public void close() throws IOException {
051                    out.close();
052            }
053    
054            /**
055             * @see java.io.OutputStream#flush()
056             */
057            public void flush() throws IOException {
058                    out.flush();
059            }
060    
061            /**
062             * @see javax.servlet.ServletOutputStream#print(boolean)
063             */
064            public void print(boolean arg0) throws IOException {
065                    print(String.valueOf(arg0));
066            }
067    
068            /**
069             * @see javax.servlet.ServletOutputStream#print(char)
070             */
071            public void print(char arg0) throws IOException {
072                    write(arg0);
073            }
074    
075            /**
076             * @see javax.servlet.ServletOutputStream#print(double)
077             */
078            public void print(double arg0) throws IOException {
079                    print(String.valueOf(arg0));
080            }
081    
082            /**
083             * @see javax.servlet.ServletOutputStream#print(float)
084             */
085            public void print(float arg0) throws IOException {
086                    print(String.valueOf(arg0));
087            }
088    
089            /**
090             * @see javax.servlet.ServletOutputStream#print(int)
091             */
092            public void print(int arg0) throws IOException {
093                    print(String.valueOf(arg0));
094            }
095    
096            /**
097             * @see javax.servlet.ServletOutputStream#print(long)
098             */
099            public void print(long arg0) throws IOException {
100                    print(String.valueOf(arg0));
101            }
102    
103            /**
104             * @see javax.servlet.ServletOutputStream#println()
105             */
106            public void println() throws IOException {
107                    write('\n');
108            }
109    
110            /**
111             * @see javax.servlet.ServletOutputStream#println(boolean)
112             */
113            public void println(boolean arg0) throws IOException {
114                    println(String.valueOf(arg0));
115            }
116    
117            /**
118             * @see javax.servlet.ServletOutputStream#println(char)
119             */
120            public void println(char arg0) throws IOException {
121                    println(String.valueOf(arg0));
122            }
123    
124            /**
125             * @see javax.servlet.ServletOutputStream#println(double)
126             */
127            public void println(double arg0) throws IOException {
128                    println(String.valueOf(arg0));
129            }
130    
131            /**
132             * @see javax.servlet.ServletOutputStream#println(float)
133             */
134            public void println(float arg0) throws IOException {
135                    println(String.valueOf(arg0));
136            }
137    
138            /**
139             * @see javax.servlet.ServletOutputStream#println(int)
140             */
141            public void println(int arg0) throws IOException {
142                    println(String.valueOf(arg0));
143            }
144    
145            /**
146             * @see javax.servlet.ServletOutputStream#println(long)
147             */
148            public void println(long arg0) throws IOException {
149                    println(String.valueOf(arg0));
150            }
151    
152            /**
153             * @see javax.servlet.ServletOutputStream#print(java.lang.String)
154             */
155            public void print(String arg0) throws IOException {
156                    int len = arg0.length();
157                    for (int i = 0; i < len; i++) {
158                            int c = arg0.charAt(i);
159                            print(c);
160                    }
161            }
162    
163            /**
164             * @see javax.servlet.ServletOutputStream#println(java.lang.String)
165             */
166            public void println(String arg0) throws IOException {
167                    print(arg0);
168                    print('\n');
169            }
170    
171            /**
172             * @see java.io.OutputStream#write(int)
173             */
174            public void write(int b) throws IOException {
175                    out.write(b);
176            }
177    
178            /**
179             * @see java.io.OutputStream#write(byte[])
180             */
181            public void write(byte[] b) throws IOException {
182                    out.write(b);
183            }
184    
185            /**
186             * @see java.io.OutputStream#write(byte[], int, int)
187             */
188            public void write(byte[] b, int off, int len) throws IOException {
189                    out.write(b, off, len);
190            }
191    }