KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > JspPrintWriter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp;
31
32 import com.caucho.log.Log;
33 import com.caucho.vfs.FlushBuffer;
34
35 import javax.servlet.jsp.JspWriter JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.PrintWriter JavaDoc;
38 import java.io.StringWriter JavaDoc;
39 import java.io.Writer JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * A buffered JSP writer encapsulating a Writer.
45  */

46 public class JspPrintWriter extends PrintWriter JavaDoc implements FlushBuffer {
47   private static final Logger JavaDoc log = Log.open(JspPrintWriter.class);
48   
49   private static final Writer JavaDoc _dummyWriter = new StringWriter JavaDoc();
50   
51   private JspWriter JavaDoc _jspWriter;
52
53   /**
54    * Creates a new JspPrintWriter
55    */

56   JspPrintWriter()
57   {
58     super(_dummyWriter);
59   }
60
61   /**
62    * Creates a new JspPrintWriter
63    */

64   JspPrintWriter(JspWriter JavaDoc jspWriter)
65   {
66     super(jspWriter);
67     
68     _jspWriter = jspWriter;
69   }
70
71   /**
72    * Initialize the JSP writer
73    *
74    * @param os the underlying stream
75    */

76   void init(JspWriter JavaDoc jspWriter)
77   {
78     _jspWriter = jspWriter;
79   }
80
81   /**
82    * Writes a character array to the writer.
83    *
84    * @param buf the buffer to write.
85    * @param off the offset into the buffer
86    * @param len the number of characters to write
87    */

88   final public void write(char []buf, int offset, int length)
89   {
90     try {
91       _jspWriter.write(buf, offset, length);
92     } catch (IOException JavaDoc e) {
93       log.log(Level.FINE, e.toString(), e);
94     }
95   }
96   
97   /**
98    * Writes a character to the output.
99    *
100    * @param buf the buffer to write.
101    */

102   final public void write(int ch)
103   {
104     try {
105       _jspWriter.write(ch);
106     } catch (IOException JavaDoc e) {
107       log.log(Level.FINE, e.toString(), e);
108     }
109   }
110
111   /**
112    * Writes a char buffer to the output.
113    *
114    * @param buf the buffer to write.
115    */

116   final public void write(char []buf)
117   {
118     try {
119       _jspWriter.write(buf, 0, buf.length);
120     } catch (IOException JavaDoc e) {
121       log.log(Level.FINE, e.toString(), e);
122     }
123   }
124
125   /**
126    * Writes a string to the output.
127    */

128   final public void write(String JavaDoc s)
129   {
130     try {
131       _jspWriter.write(s, 0, s.length());
132     } catch (IOException JavaDoc e) {
133       log.log(Level.FINE, e.toString(), e);
134     }
135   }
136
137   /**
138    * Writes a subsection of a string to the output.
139    */

140   final public void write(String JavaDoc s, int off, int len)
141   {
142     try {
143       _jspWriter.write(s, 0, s.length());
144     } catch (IOException JavaDoc e) {
145       log.log(Level.FINE, e.toString(), e);
146     }
147   }
148
149   /**
150    * Writes the newline character.
151    */

152   final public void newLine()
153   {
154     try {
155       _jspWriter.newLine();
156     } catch (IOException JavaDoc e) {
157       log.log(Level.FINE, e.toString(), e);
158     }
159   }
160   
161   /**
162    * Prints a boolean.
163    */

164   final public void print(boolean b)
165   {
166     try {
167       _jspWriter.print(b);
168     } catch (IOException JavaDoc e) {
169       log.log(Level.FINE, e.toString(), e);
170     }
171   }
172
173   /**
174    * Prints a character.
175    */

176   final public void print(char ch)
177   {
178     try {
179       _jspWriter.write(ch);
180     } catch (IOException JavaDoc e) {
181       log.log(Level.FINE, e.toString(), e);
182     }
183   }
184
185   /**
186    * Prints an integer.
187    */

188   final public void print(int v)
189   {
190     try {
191       _jspWriter.print(v);
192     } catch (IOException JavaDoc e) {
193       log.log(Level.FINE, e.toString(), e);
194     }
195   }
196   
197   /**
198    * Prints a long
199    */

200   final public void print(long v)
201   {
202     try {
203       _jspWriter.print(v);
204     } catch (IOException JavaDoc e) {
205       log.log(Level.FINE, e.toString(), e);
206     }
207   }
208   
209   /**
210    * Prints a float
211    */

212   final public void print(float f)
213   {
214     try {
215       _jspWriter.print(f);
216     } catch (IOException JavaDoc e) {
217       log.log(Level.FINE, e.toString(), e);
218     }
219   }
220
221   /**
222    * Prints a double.
223    */

224   final public void print(double d)
225   {
226     try {
227       _jspWriter.print(d);
228     } catch (IOException JavaDoc e) {
229       log.log(Level.FINE, e.toString(), e);
230     }
231   }
232
233   /**
234    * Prints a character array
235    */

236   final public void print(char []s)
237   {
238     try {
239       _jspWriter.write(s, 0, s.length);
240     } catch (IOException JavaDoc e) {
241       log.log(Level.FINE, e.toString(), e);
242     }
243   }
244
245   /**
246    * Prints a string.
247    */

248   final public void print(String JavaDoc s)
249   {
250     try {
251       _jspWriter.print(s);
252     } catch (IOException JavaDoc e) {
253       log.log(Level.FINE, e.toString(), e);
254     }
255   }
256
257   /**
258    * Prints the value of the object.
259    */

260   final public void print(Object JavaDoc v)
261   {
262     try {
263       _jspWriter.print(v);
264     } catch (IOException JavaDoc e) {
265       log.log(Level.FINE, e.toString(), e);
266     }
267   }
268
269   /**
270    * Prints the newline.
271    */

272   final public void println()
273   {
274     try {
275       _jspWriter.newLine();
276     } catch (IOException JavaDoc e) {
277       log.log(Level.FINE, e.toString(), e);
278     }
279   }
280   
281   /**
282    * Prints the boolean followed by a newline.
283    *
284    * @param v the value to print
285    */

286   final public void println(boolean v)
287   {
288     try {
289       _jspWriter.println(v);
290     } catch (IOException JavaDoc e) {
291       log.log(Level.FINE, e.toString(), e);
292     }
293   }
294
295   /**
296    * Prints a character followed by a newline.
297    *
298    * @param v the value to print
299    */

300   final public void println(char v)
301   {
302     try {
303       _jspWriter.println(v);
304     } catch (IOException JavaDoc e) {
305       log.log(Level.FINE, e.toString(), e);
306     }
307   }
308   
309   /**
310    * Prints an integer followed by a newline.
311    *
312    * @param v the value to print
313    */

314   final public void println(int v)
315   {
316     try {
317       _jspWriter.println(v);
318     } catch (IOException JavaDoc e) {
319       log.log(Level.FINE, e.toString(), e);
320     }
321   }
322   
323   /**
324    * Prints a long followed by a newline.
325    *
326    * @param v the value to print
327    */

328   final public void println(long v)
329   {
330     try {
331       _jspWriter.println(v);
332     } catch (IOException JavaDoc e) {
333       log.log(Level.FINE, e.toString(), e);
334     }
335   }
336   
337   /**
338    * Prints a float followed by a newline.
339    *
340    * @param v the value to print
341    */

342   final public void println(float v)
343   {
344     try {
345       _jspWriter.println(v);
346     } catch (IOException JavaDoc e) {
347       log.log(Level.FINE, e.toString(), e);
348     }
349   }
350   
351   
352   /**
353    * Prints a double followed by a newline.
354    *
355    * @param v the value to print
356    */

357   final public void println(double v)
358   {
359     try {
360       _jspWriter.println(v);
361     } catch (IOException JavaDoc e) {
362       log.log(Level.FINE, e.toString(), e);
363     }
364   }
365
366   /**
367    * Writes a character array followed by a newline.
368    */

369   final public void println(char []s)
370   {
371     try {
372       _jspWriter.println(s);
373     } catch (IOException JavaDoc e) {
374       log.log(Level.FINE, e.toString(), e);
375     }
376   }
377
378   /**
379    * Writes a string followed by a newline.
380    */

381   final public void println(String JavaDoc s)
382   {
383     try {
384       _jspWriter.println(s);
385     } catch (IOException JavaDoc e) {
386       log.log(Level.FINE, e.toString(), e);
387     }
388   }
389   
390   /**
391    * Writes an object followed by a newline.
392    */

393   final public void println(Object JavaDoc v)
394   {
395     try {
396       _jspWriter.println(v);
397     } catch (IOException JavaDoc e) {
398       log.log(Level.FINE, e.toString(), e);
399     }
400   }
401
402   /**
403    * Flushes the buffer and the writer.
404    */

405   public void flushBuffer()
406   {
407     try {
408       if (_jspWriter instanceof FlushBuffer)
409     ((FlushBuffer) _jspWriter).flushBuffer();
410       else
411     _jspWriter.flush();
412     } catch (IOException JavaDoc e) {
413       log.log(Level.FINE, e.toString(), e);
414     }
415   }
416
417   /**
418    * Flushes the buffer and the writer.
419    */

420   public void flush()
421   {
422     try {
423       _jspWriter.flush();
424     } catch (IOException JavaDoc e) {
425       log.log(Level.FINE, e.toString(), e);
426     }
427   }
428
429   final public void clear()
430   {
431     try {
432       _jspWriter.clear();
433     } catch (IOException JavaDoc e) {
434       log.log(Level.FINE, e.toString(), e);
435     }
436   }
437
438   final public void close()
439   {
440     try {
441       _jspWriter.close();
442     } catch (IOException JavaDoc e) {
443       log.log(Level.FINE, e.toString(), e);
444     }
445   }
446 }
447
Popular Tags