KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > PrintWriterImpl


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.vfs;
30
31 import java.io.IOException JavaDoc;
32 import java.io.PrintWriter JavaDoc;
33 import java.io.StringWriter JavaDoc;
34 import java.io.Writer JavaDoc;
35 import java.util.logging.Level JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 /**
39  * A print writer which allows for a changing writer.
40  */

41 public class PrintWriterImpl extends PrintWriter JavaDoc implements FlushBuffer {
42   private final static Logger JavaDoc log
43     = Logger.getLogger(PrintWriterImpl.class.getName());
44   
45   private final static char []_nullChars = "null".toCharArray();
46   private final static char []_newline = "\n".toCharArray();
47
48   private final static Writer JavaDoc _dummyWriter = new StringWriter();
49   
50   private final char []_tempCharBuffer = new char[64];
51   
52   /**
53    * Creates a new PrintWriterImpl
54    */

55   public PrintWriterImpl()
56   {
57     super((Writer JavaDoc) _dummyWriter);
58   }
59
60   /**
61    * Creates a new PrintWriterImpl
62    */

63   public PrintWriterImpl(Writer JavaDoc out)
64   {
65     super(out);
66   }
67
68   /**
69    * Sets the underlying writer.
70    */

71   public void setWriter(Writer JavaDoc out)
72   {
73     this.out = out;
74   }
75
76   /**
77    * Writes a character.
78    */

79   final public void write(char ch)
80   {
81     Writer JavaDoc out = this.out;
82     if (out == null)
83       return;
84     
85     try {
86       out.write(ch);
87     } catch (IOException JavaDoc e) {
88       log.log(Level.FINE, e.toString(), e);
89     }
90   }
91
92   /**
93    * Writes a character.
94    */

95   final public void write(char []buf, int offset, int length)
96   {
97     Writer JavaDoc out = this.out;
98     if (out == null)
99       return;
100
101     try {
102       out.write(buf, offset, length);
103     } catch (IOException JavaDoc e) {
104       log.log(Level.FINE, e.toString(), e);
105     }
106   }
107
108   /**
109    * Writes a character buffer.
110    */

111   final public void write(char []buf)
112   {
113     Writer JavaDoc out = this.out;
114     if (out == null)
115       return;
116
117     try {
118       out.write(buf, 0, buf.length);
119     } catch (IOException JavaDoc e) {
120       log.log(Level.FINE, e.toString(), e);
121     }
122   }
123
124   /**
125    * Prints a character.
126    */

127   final public void print(char ch)
128   {
129     Writer JavaDoc out = this.out;
130     if (out == null)
131       return;
132
133     try {
134       out.write(ch);
135     } catch (IOException JavaDoc e) {
136       log.log(Level.FINE, e.toString(), e);
137     }
138   }
139
140   /**
141    * Prints an integer.
142    */

143   final public void print(int i)
144   {
145     Writer JavaDoc out = this.out;
146     if (out == null)
147       return;
148     
149     if (i == 0x80000000) {
150       print("-2147483648");
151       return;
152     }
153
154     try {
155       if (i < 0) {
156     out.write('-');
157     i = -i;
158       } else if (i < 9) {
159     out.write('0' + i);
160     return;
161       }
162
163       int length = 0;
164       int exp = 10;
165
166       if (i >= 1000000000)
167     length = 9;
168       else {
169     for (; i >= exp; length++)
170       exp = 10 * exp;
171       }
172
173       int j = 31;
174     
175       while (i > 0) {
176     _tempCharBuffer[--j] = (char) ((i % 10) + '0');
177     i /= 10;
178       }
179
180       out.write(_tempCharBuffer, j, 31 - j);
181     } catch (IOException JavaDoc e) {
182       log.log(Level.FINE, e.toString(), e);
183     }
184   }
185   
186   /**
187    * Prints a long.
188    */

189   final public void print(long v)
190   {
191     Writer JavaDoc out = this.out;
192     if (out == null)
193       return;
194     
195     if (v == 0x8000000000000000L) {
196       print("-9223372036854775808");
197       return;
198     }
199
200     try {
201       if (v < 0) {
202     out.write('-');
203     v = -v;
204       } else if (v == 0) {
205     out.write('0');
206     return;
207       }
208
209       int j = 31;
210     
211       while (v > 0) {
212     _tempCharBuffer[--j] = (char) ((v % 10) + '0');
213     v /= 10;
214       }
215
216       out.write(_tempCharBuffer, j, 31 - j);
217     } catch (IOException JavaDoc e) {
218       log.log(Level.FINE, e.toString(), e);
219     }
220   }
221   
222   /**
223    * Prints a double followed by a newline.
224    *
225    * @param v the value to print
226    */

227   final public void print(float v)
228   {
229     Writer JavaDoc out = this.out;
230     if (out == null)
231       return;
232
233     try {
234       String JavaDoc s = String.valueOf(v);
235       out.write(s, 0, s.length());
236     } catch (IOException JavaDoc e) {
237       log.log(Level.FINE, e.toString(), e);
238     }
239   }
240   
241   /**
242    * Prints a double followed by a newline.
243    *
244    * @param v the value to print
245    */

246   final public void print(double v)
247   {
248     Writer JavaDoc out = this.out;
249     if (out == null)
250       return;
251
252     try {
253       String JavaDoc s = String.valueOf(v);
254       out.write(s, 0, s.length());
255     } catch (IOException JavaDoc e) {
256       log.log(Level.FINE, e.toString(), e);
257     }
258   }
259
260   /**
261    * Prints a character array
262    */

263   final public void print(char []s)
264   {
265     Writer JavaDoc out = this.out;
266     if (out == null)
267       return;
268
269     try {
270       out.write(s, 0, s.length);
271     } catch (IOException JavaDoc e) {
272       log.log(Level.FINE, e.toString(), e);
273     }
274   }
275
276   /**
277    * Prints a string.
278    */

279   final public void print(String JavaDoc s)
280   {
281     Writer JavaDoc out = this.out;
282     if (out == null)
283       return;
284
285     try {
286       if (s == null)
287     out.write(_nullChars, 0, _nullChars.length);
288       else
289     out.write(s, 0, s.length());
290     } catch (IOException JavaDoc e) {
291       log.log(Level.FINE, e.toString(), e);
292     }
293   }
294
295   /**
296    * Prints the value of the object.
297    */

298   final public void print(Object JavaDoc v)
299   {
300     Writer JavaDoc out = this.out;
301     if (out == null)
302       return;
303
304     try {
305       if (v == null)
306     out.write(_nullChars, 0, _nullChars.length);
307       else {
308     String JavaDoc s = v.toString();
309       
310     out.write(s, 0, s.length());
311       }
312     } catch (IOException JavaDoc e) {
313       log.log(Level.FINE, e.toString(), e);
314     }
315   }
316
317   /**
318    * Prints the newline.
319    */

320   final public void println()
321   {
322     Writer JavaDoc out = this.out;
323     if (out == null)
324       return;
325
326     try {
327       out.write(_newline, 0, _newline.length);
328     } catch (IOException JavaDoc e) {
329       log.log(Level.FINE, e.toString(), e);
330     }
331   }
332   
333   /**
334    * Prints the boolean followed by a newline.
335    *
336    * @param v the value to print
337    */

338   final public void println(boolean v)
339   {
340     Writer JavaDoc out = this.out;
341     if (out == null)
342       return;
343
344     print(v);
345
346     try {
347       out.write(_newline, 0, _newline.length);
348     } catch (IOException JavaDoc e) {
349       log.log(Level.FINE, e.toString(), e);
350     }
351   }
352
353   /**
354    * Prints a character followed by a newline.
355    *
356    * @param v the value to print
357    */

358   final public void println(char v)
359   {
360     Writer JavaDoc out = this.out;
361     if (out == null)
362       return;
363
364     try {
365       out.write(v);
366       out.write(_newline, 0, _newline.length);
367     } catch (IOException JavaDoc e) {
368       log.log(Level.FINE, e.toString(), e);
369     }
370   }
371   
372   /**
373    * Prints an integer followed by a newline.
374    *
375    * @param v the value to print
376    */

377   final public void println(int v)
378   {
379     Writer JavaDoc out = this.out;
380     if (out == null)
381       return;
382
383     print(v);
384
385     try {
386       out.write(_newline, 0, _newline.length);
387     } catch (IOException JavaDoc e) {
388       log.log(Level.FINE, e.toString(), e);
389     }
390   }
391   
392   /**
393    * Prints a long followed by a newline.
394    *
395    * @param v the value to print
396    */

397   final public void println(long v)
398   {
399     Writer JavaDoc out = this.out;
400     if (out == null)
401       return;
402
403     print(v);
404
405     try {
406       out.write(_newline, 0, _newline.length);
407     } catch (IOException JavaDoc e) {
408       log.log(Level.FINE, e.toString(), e);
409     }
410   }
411   
412   /**
413    * Prints a float followed by a newline.
414    *
415    * @param v the value to print
416    */

417   final public void println(float v)
418   {
419     Writer JavaDoc out = this.out;
420     if (out == null)
421       return;
422
423     String JavaDoc s = String.valueOf(v);
424     
425     try {
426       out.write(s, 0, s.length());
427       out.write(_newline, 0, _newline.length);
428     } catch (IOException JavaDoc e) {
429       log.log(Level.FINE, e.toString(), e);
430     }
431   }
432   
433   /**
434    * Prints a double followed by a newline.
435    *
436    * @param v the value to print
437    */

438   final public void println(double v)
439   {
440     Writer JavaDoc out = this.out;
441     if (out == null)
442       return;
443
444     print(v);
445     
446     try {
447       out.write(_newline, 0, _newline.length);
448     } catch (IOException JavaDoc e) {
449       log.log(Level.FINE, e.toString(), e);
450     }
451   }
452
453   /**
454    * Writes a character array followed by a newline.
455    */

456   final public void println(char []s)
457   {
458     Writer JavaDoc out = this.out;
459     if (out == null)
460       return;
461
462     try {
463       out.write(s, 0, s.length);
464       out.write(_newline, 0, _newline.length);
465     } catch (IOException JavaDoc e) {
466       log.log(Level.FINE, e.toString(), e);
467     }
468   }
469
470   /**
471    * Writes a string followed by a newline.
472    */

473   final public void println(String JavaDoc s)
474   {
475     Writer JavaDoc out = this.out;
476     if (out == null)
477       return;
478
479     try {
480       if (s == null)
481     out.write(_nullChars, 0, _nullChars.length);
482       else
483     out.write(s, 0, s.length());
484
485       out.write(_newline, 0, _newline.length);
486     } catch (IOException JavaDoc e) {
487       log.log(Level.FINE, e.toString(), e);
488     }
489   }
490   
491   /**
492    * Writes an object followed by a newline.
493    */

494   final public void println(Object JavaDoc v)
495   {
496     Writer JavaDoc out = this.out;
497     if (out == null)
498       return;
499
500     try {
501       if (v == null)
502     out.write(_nullChars, 0, _nullChars.length);
503       else {
504     String JavaDoc s = v.toString();
505
506     out.write(s, 0, s.length());
507       }
508     
509       out.write(_newline, 0, _newline.length);
510     } catch (IOException JavaDoc e) {
511       log.log(Level.FINE, e.toString(), e);
512     }
513   }
514
515   /**
516    * Flushes the writer.
517    */

518   public void flush()
519   {
520     Writer JavaDoc out = this.out;
521     if (out == null)
522       return;
523
524     try {
525       out.flush();
526     } catch (IOException JavaDoc e) {
527       log.log(Level.FINE, e.toString(), e);
528     }
529   }
530
531   /**
532    * Flushes the writer.
533    */

534   public void flushBuffer()
535   {
536     Writer JavaDoc out = this.out;
537     if (out == null)
538       return;
539
540     try {
541       if (out instanceof FlushBuffer)
542     ((FlushBuffer) out).flushBuffer();
543     } catch (IOException JavaDoc e) {
544       log.log(Level.FINE, e.toString(), e);
545     }
546   }
547
548   public void close()
549   {
550     this.out = null;
551   }
552 }
553
Popular Tags