KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > applications > StringServletOutputStream


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 22-AUG-2003, Jahia Solutions Sarl, Fulco Houkes
37  *
38  * ----- END LICENSE BLOCK -----
39  */

40
41 package org.jahia.services.applications;
42
43 import javax.servlet.ServletOutputStream JavaDoc;
44 import java.io.ByteArrayOutputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.OutputStreamWriter JavaDoc;
47 import java.io.UnsupportedEncodingException JavaDoc;
48
49 /**
50  * A simple ServletOutputStream based class that writes to a String and
51  * simultaneously to an output stream if configured.
52  * Note : none of this is supposed to be optimised in anyway, it was done a
53  * quick and dirty wayusing mostly already available functions calls to make
54  * this reliable.
55  *
56  * @author : Serge Huber
57  * @todo Test this code with Orion, WebLogic, etc... This might be very
58  * container-sensitive code.
59  */

60 public class StringServletOutputStream extends ServletOutputStream JavaDoc {
61
62     private static org.apache.log4j.Logger logger =
63             org.apache.log4j.Logger.getLogger (StringServletOutputStream.class);
64
65     private ByteArrayOutputStream JavaDoc byteArray = new ByteArrayOutputStream JavaDoc ();
66     private OutputStreamWriter JavaDoc streamWriter;
67     private ServletOutputStream JavaDoc existingStream;
68     private OutputStreamWriter JavaDoc existingStreamWriter;
69     private String JavaDoc encoding;
70
71     public StringServletOutputStream (String JavaDoc encoding) throws UnsupportedEncodingException JavaDoc {
72         super ();
73         this.encoding = encoding;
74         streamWriter = new OutputStreamWriter JavaDoc (byteArray, encoding);
75     }
76
77     public StringServletOutputStream (ServletOutputStream JavaDoc existingStream, String JavaDoc encoding)
78             throws UnsupportedEncodingException JavaDoc {
79         super ();
80         this.existingStream = existingStream;
81         this.encoding = encoding;
82         streamWriter = new OutputStreamWriter JavaDoc (byteArray, encoding);
83         existingStreamWriter = new OutputStreamWriter JavaDoc (existingStream, encoding);
84     }
85
86     /*
87     public void print(boolean b) {
88         try {
89             Boolean bol = new Boolean(b);
90             streamWriter.write(bol.toString());
91         } catch (java.io.IOException ioe) {
92             logger.warn (ioe);
93         }
94     }
95
96     public void print(char c) {
97         try {
98             streamWriter.write(c);
99         } catch (java.io.IOException ioe) {
100             logger.warn (ioe);
101         }
102     }
103
104     public void print(char[] s) {
105         try {
106             streamWriter.write(s);
107         } catch (java.io.IOException ioe) {
108             logger.warn (ioe);
109         }
110     }
111
112     public void print(double d) {
113         try {
114             streamWriter.write(Double.toString(d));
115         } catch (java.io.IOException ioe) {
116             logger.warn (ioe);
117         }
118     }
119
120     public void print(float f) {
121         try {
122             streamWriter.write(Float.toString(f));
123         } catch (java.io.IOException ioe) {
124             logger.warn (ioe);
125         }
126     }
127
128     public void print(int i) {
129         try {
130             streamWriter.write(i);
131         } catch (java.io.IOException ioe) {
132             logger.warn (ioe);
133         }
134     }
135
136     public void print(long l) {
137         try {
138             streamWriter.write(Long.toString(l));
139         } catch (java.io.IOException ioe) {
140             logger.warn (ioe);
141         }
142     }
143
144     public void print(String s) {
145         try {
146             streamWriter.write(s);
147         } catch (java.io.IOException ioe) {
148             logger.warn (ioe);
149         }
150     }
151
152     public void println() {
153         try {
154             streamWriter.write("\n");
155         } catch (java.io.IOException ioe) {
156             logger.warn (ioe);
157         }
158     }
159
160     public void println(boolean b) {
161         this.print(b);
162         this.println();
163     }
164
165     public void println(char c) {
166         this.print(c);
167         this.println();
168     }
169
170     public void println(char[] s) {
171         this.print(s);
172         this.println();
173     }
174
175     public void println(double d) {
176         this.print(d);
177         this.println();
178     }
179
180     public void println(float f) {
181         this.print(f);
182         this.println();
183     }
184
185     public void println(int i) {
186         this.print(i);
187         this.println();
188     }
189
190     public void println(long l) {
191         this.print(l);
192         this.println();
193     }
194
195
196     public void println(String x) {
197         this.print(x);
198         this.println();
199     }
200 */

201     public void write (char[] cbuf)
202             throws IOException JavaDoc {
203         streamWriter.write (cbuf, 0, cbuf.length);
204         if (existingStream != null) {
205             existingStreamWriter.write (cbuf, 0, cbuf.length);
206         }
207     }
208
209     public void write (char[] cbuf, int off, int len)
210             throws IOException JavaDoc {
211         streamWriter.write (cbuf, off, len);
212         if (existingStream != null) {
213             existingStreamWriter.write (cbuf, off, len);
214         }
215     }
216
217     public void write (int c)
218             throws IOException JavaDoc {
219         byteArray.write (c);
220         if (existingStream != null) {
221             existingStream.write (c);
222         }
223     }
224
225     public void write (String JavaDoc s)
226             throws IOException JavaDoc {
227         streamWriter.write (s);
228         if (existingStream != null) {
229             existingStreamWriter.write (s);
230         }
231     }
232
233     public void write (String JavaDoc str, int off, int len)
234             throws IOException JavaDoc {
235         streamWriter.write (str, off, len);
236         if (existingStream != null) {
237             existingStreamWriter.write (str, off, len);
238         }
239     }
240
241     public String JavaDoc getBuffer () throws UnsupportedEncodingException JavaDoc {
242         return byteArray.toString (encoding);
243     }
244
245     public void flush ()
246             throws IOException JavaDoc {
247         if (existingStream != null) {
248             logger.debug ("Flushing pass-through stream...");
249             existingStreamWriter.flush ();
250             existingStream.flush ();
251         }
252     }
253
254     public void close () {
255     }
256
257 }
258
Popular Tags