001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jsp;
031:
032: import com.caucho.log.Log;
033: import com.caucho.vfs.FlushBuffer;
034:
035: import javax.servlet.jsp.JspWriter;
036: import java.io.IOException;
037: import java.io.PrintWriter;
038: import java.io.StringWriter;
039: import java.io.Writer;
040: import java.util.logging.Level;
041: import java.util.logging.Logger;
042:
043: /**
044: * A buffered JSP writer encapsulating a Writer.
045: */
046: public class JspPrintWriter extends PrintWriter implements FlushBuffer {
047: private static final Logger log = Log.open(JspPrintWriter.class);
048:
049: private static final Writer _dummyWriter = new StringWriter();
050:
051: private JspWriter _jspWriter;
052:
053: /**
054: * Creates a new JspPrintWriter
055: */
056: JspPrintWriter() {
057: super (_dummyWriter);
058: }
059:
060: /**
061: * Creates a new JspPrintWriter
062: */
063: JspPrintWriter(JspWriter jspWriter) {
064: super (jspWriter);
065:
066: _jspWriter = jspWriter;
067: }
068:
069: /**
070: * Initialize the JSP writer
071: *
072: * @param os the underlying stream
073: */
074: void init(JspWriter jspWriter) {
075: _jspWriter = jspWriter;
076: }
077:
078: /**
079: * Writes a character array to the writer.
080: *
081: * @param buf the buffer to write.
082: * @param off the offset into the buffer
083: * @param len the number of characters to write
084: */
085: final public void write(char[] buf, int offset, int length) {
086: try {
087: _jspWriter.write(buf, offset, length);
088: } catch (IOException e) {
089: log.log(Level.FINE, e.toString(), e);
090: }
091: }
092:
093: /**
094: * Writes a character to the output.
095: *
096: * @param buf the buffer to write.
097: */
098: final public void write(int ch) {
099: try {
100: _jspWriter.write(ch);
101: } catch (IOException e) {
102: log.log(Level.FINE, e.toString(), e);
103: }
104: }
105:
106: /**
107: * Writes a char buffer to the output.
108: *
109: * @param buf the buffer to write.
110: */
111: final public void write(char[] buf) {
112: try {
113: _jspWriter.write(buf, 0, buf.length);
114: } catch (IOException e) {
115: log.log(Level.FINE, e.toString(), e);
116: }
117: }
118:
119: /**
120: * Writes a string to the output.
121: */
122: final public void write(String s) {
123: try {
124: _jspWriter.write(s, 0, s.length());
125: } catch (IOException e) {
126: log.log(Level.FINE, e.toString(), e);
127: }
128: }
129:
130: /**
131: * Writes a subsection of a string to the output.
132: */
133: final public void write(String s, int off, int len) {
134: try {
135: _jspWriter.write(s, 0, s.length());
136: } catch (IOException e) {
137: log.log(Level.FINE, e.toString(), e);
138: }
139: }
140:
141: /**
142: * Writes the newline character.
143: */
144: final public void newLine() {
145: try {
146: _jspWriter.newLine();
147: } catch (IOException e) {
148: log.log(Level.FINE, e.toString(), e);
149: }
150: }
151:
152: /**
153: * Prints a boolean.
154: */
155: final public void print(boolean b) {
156: try {
157: _jspWriter.print(b);
158: } catch (IOException e) {
159: log.log(Level.FINE, e.toString(), e);
160: }
161: }
162:
163: /**
164: * Prints a character.
165: */
166: final public void print(char ch) {
167: try {
168: _jspWriter.write(ch);
169: } catch (IOException e) {
170: log.log(Level.FINE, e.toString(), e);
171: }
172: }
173:
174: /**
175: * Prints an integer.
176: */
177: final public void print(int v) {
178: try {
179: _jspWriter.print(v);
180: } catch (IOException e) {
181: log.log(Level.FINE, e.toString(), e);
182: }
183: }
184:
185: /**
186: * Prints a long
187: */
188: final public void print(long v) {
189: try {
190: _jspWriter.print(v);
191: } catch (IOException e) {
192: log.log(Level.FINE, e.toString(), e);
193: }
194: }
195:
196: /**
197: * Prints a float
198: */
199: final public void print(float f) {
200: try {
201: _jspWriter.print(f);
202: } catch (IOException e) {
203: log.log(Level.FINE, e.toString(), e);
204: }
205: }
206:
207: /**
208: * Prints a double.
209: */
210: final public void print(double d) {
211: try {
212: _jspWriter.print(d);
213: } catch (IOException e) {
214: log.log(Level.FINE, e.toString(), e);
215: }
216: }
217:
218: /**
219: * Prints a character array
220: */
221: final public void print(char[] s) {
222: try {
223: _jspWriter.write(s, 0, s.length);
224: } catch (IOException e) {
225: log.log(Level.FINE, e.toString(), e);
226: }
227: }
228:
229: /**
230: * Prints a string.
231: */
232: final public void print(String s) {
233: try {
234: _jspWriter.print(s);
235: } catch (IOException e) {
236: log.log(Level.FINE, e.toString(), e);
237: }
238: }
239:
240: /**
241: * Prints the value of the object.
242: */
243: final public void print(Object v) {
244: try {
245: _jspWriter.print(v);
246: } catch (IOException e) {
247: log.log(Level.FINE, e.toString(), e);
248: }
249: }
250:
251: /**
252: * Prints the newline.
253: */
254: final public void println() {
255: try {
256: _jspWriter.newLine();
257: } catch (IOException e) {
258: log.log(Level.FINE, e.toString(), e);
259: }
260: }
261:
262: /**
263: * Prints the boolean followed by a newline.
264: *
265: * @param v the value to print
266: */
267: final public void println(boolean v) {
268: try {
269: _jspWriter.println(v);
270: } catch (IOException e) {
271: log.log(Level.FINE, e.toString(), e);
272: }
273: }
274:
275: /**
276: * Prints a character followed by a newline.
277: *
278: * @param v the value to print
279: */
280: final public void println(char v) {
281: try {
282: _jspWriter.println(v);
283: } catch (IOException e) {
284: log.log(Level.FINE, e.toString(), e);
285: }
286: }
287:
288: /**
289: * Prints an integer followed by a newline.
290: *
291: * @param v the value to print
292: */
293: final public void println(int v) {
294: try {
295: _jspWriter.println(v);
296: } catch (IOException e) {
297: log.log(Level.FINE, e.toString(), e);
298: }
299: }
300:
301: /**
302: * Prints a long followed by a newline.
303: *
304: * @param v the value to print
305: */
306: final public void println(long v) {
307: try {
308: _jspWriter.println(v);
309: } catch (IOException e) {
310: log.log(Level.FINE, e.toString(), e);
311: }
312: }
313:
314: /**
315: * Prints a float followed by a newline.
316: *
317: * @param v the value to print
318: */
319: final public void println(float v) {
320: try {
321: _jspWriter.println(v);
322: } catch (IOException e) {
323: log.log(Level.FINE, e.toString(), e);
324: }
325: }
326:
327: /**
328: * Prints a double followed by a newline.
329: *
330: * @param v the value to print
331: */
332: final public void println(double v) {
333: try {
334: _jspWriter.println(v);
335: } catch (IOException e) {
336: log.log(Level.FINE, e.toString(), e);
337: }
338: }
339:
340: /**
341: * Writes a character array followed by a newline.
342: */
343: final public void println(char[] s) {
344: try {
345: _jspWriter.println(s);
346: } catch (IOException e) {
347: log.log(Level.FINE, e.toString(), e);
348: }
349: }
350:
351: /**
352: * Writes a string followed by a newline.
353: */
354: final public void println(String s) {
355: try {
356: _jspWriter.println(s);
357: } catch (IOException e) {
358: log.log(Level.FINE, e.toString(), e);
359: }
360: }
361:
362: /**
363: * Writes an object followed by a newline.
364: */
365: final public void println(Object v) {
366: try {
367: _jspWriter.println(v);
368: } catch (IOException e) {
369: log.log(Level.FINE, e.toString(), e);
370: }
371: }
372:
373: /**
374: * Flushes the buffer and the writer.
375: */
376: public void flushBuffer() {
377: try {
378: if (_jspWriter instanceof FlushBuffer)
379: ((FlushBuffer) _jspWriter).flushBuffer();
380: else
381: _jspWriter.flush();
382: } catch (IOException e) {
383: log.log(Level.FINE, e.toString(), e);
384: }
385: }
386:
387: /**
388: * Flushes the buffer and the writer.
389: */
390: public void flush() {
391: try {
392: _jspWriter.flush();
393: } catch (IOException e) {
394: log.log(Level.FINE, e.toString(), e);
395: }
396: }
397:
398: final public void clear() {
399: try {
400: _jspWriter.clear();
401: } catch (IOException e) {
402: log.log(Level.FINE, e.toString(), e);
403: }
404: }
405:
406: final public void close() {
407: try {
408: _jspWriter.close();
409: } catch (IOException e) {
410: log.log(Level.FINE, e.toString(), e);
411: }
412: }
413: }
|