KickJava   Java API By Example, From Geeks To Geeks.

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


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.PrintWriter JavaDoc;
32 import java.io.StringWriter JavaDoc;
33 import java.io.Writer JavaDoc;
34
35 /**
36  * An abstract print writer.
37  */

38 public abstract class AbstractPrintWriter extends PrintWriter JavaDoc {
39   private static final char []_trueChars = "true".toCharArray();
40   private static final char []_falseChars = "false".toCharArray();
41   private static final char []_nullChars = "null".toCharArray();
42
43   private static final Writer JavaDoc _dummyWriter = new StringWriter();
44
45   private final char []_tempCharBuffer = new char[64];
46
47   /**
48    * Creates the print writer.
49    */

50   protected AbstractPrintWriter()
51   {
52     super(_dummyWriter);
53   }
54
55   /**
56    * Writes a character array to the writer.
57    *
58    * @param buf the buffer to write.
59    * @param off the offset into the buffer
60    * @param len the number of characters to write
61    */

62   abstract public void write(char []buf, int offset, int length);
63   
64   /**
65    * Writes a character to the output.
66    *
67    * @param buf the buffer to write.
68    */

69   abstract public void write(int ch);
70
71   /**
72    * Writes a subsection of a string to the output.
73    */

74   abstract public void write(String JavaDoc s, int off, int len);
75
76   /**
77    * Writes a char buffer to the output.
78    *
79    * @param buf the buffer to write.
80    */

81   final public void write(char []buf)
82   {
83     write(buf, 0, buf.length);
84   }
85
86   /**
87    * Writes a string to the output.
88    */

89   final public void write(String JavaDoc s)
90   {
91     write(s, 0, s.length());
92   }
93
94   /**
95    * Writes the newline character.
96    */

97   public void newLine()
98   {
99     write('\n');
100   }
101   
102   /**
103    * Prints a boolean.
104    */

105   final public void print(boolean b)
106   {
107     write(b ? _trueChars : _falseChars);
108   }
109
110   /**
111    * Prints a character.
112    */

113   public void print(char ch)
114   {
115     write(ch);
116   }
117
118   /**
119    * Prints an integer value.
120    */

121   public void print(int i)
122   {
123     if (i == 0x80000000) {
124       print("-2147483648");
125       return;
126     }
127
128     if (i < 0) {
129       write('-');
130       i = -i;
131     } else if (i < 9) {
132       write('0' + i);
133       return;
134     }
135
136     int length = 0;
137     int exp = 10;
138
139     if (i >= 1000000000)
140       length = 9;
141     else {
142       for (; i >= exp; length++)
143         exp = 10 * exp;
144     }
145
146     int j = 31;
147     
148     while (i > 0) {
149       _tempCharBuffer[--j] = (char) ((i % 10) + '0');
150       i /= 10;
151     }
152
153     write(_tempCharBuffer, j, 31 - j);
154   }
155   
156   /**
157    * Prints a long value.
158    */

159   public void print(long v)
160   {
161     if (v == 0x8000000000000000L) {
162       print("-9223372036854775808");
163       return;
164     }
165
166     if (v < 0) {
167       write('-');
168       v = -v;
169     } else if (v == 0) {
170       write('0');
171       return;
172     }
173
174     int j = 31;
175     
176     while (v > 0) {
177       _tempCharBuffer[--j] = (char) ((v % 10) + '0');
178       v /= 10;
179     }
180
181     write(_tempCharBuffer, j, 31 - j);
182   }
183   
184   final public void print(float f)
185   {
186     write(String.valueOf(f));
187   }
188   
189   final public void print(double d)
190   {
191     write(String.valueOf(d));
192   }
193
194   /**
195    * Prints a character array
196    */

197   final public void print(char []s)
198   {
199     write(s, 0, s.length);
200   }
201
202   /**
203    * Prints a string.
204    */

205   final public void print(String JavaDoc s)
206   {
207     if (s == null)
208       write(_nullChars, 0, _nullChars.length);
209     else
210       write(s, 0, s.length());
211   }
212
213   /**
214    * Prints the value of the object.
215    */

216   final public void print(Object JavaDoc v)
217   {
218     if (v == null)
219       write(_nullChars, 0, _nullChars.length);
220     else {
221       String JavaDoc s = v.toString();
222       
223       write(s, 0, s.length());
224     }
225   }
226
227   /**
228    * Prints the newline.
229    */

230   public void println()
231   {
232     write('\n');
233   }
234   
235   /**
236    * Prints the boolean followed by a newline.
237    *
238    * @param v the value to print
239    */

240   final public void println(boolean v)
241   {
242     print(v);
243     println();
244   }
245
246   /**
247    * Prints a character followed by a newline.
248    *
249    * @param v the value to print
250    */

251   final public void println(char v)
252   {
253     print(v);
254     println();
255   }
256   
257   /**
258    * Prints an integer followed by a newline.
259    *
260    * @param v the value to print
261    */

262   final public void println(int v)
263   {
264     print(v);
265     println();
266   }
267   
268   /**
269    * Prints a long followed by a newline.
270    *
271    * @param v the value to print
272    */

273   final public void println(long v)
274   {
275     print(v);
276     println();
277   }
278   
279   /**
280    * Prints a float followed by a newline.
281    *
282    * @param v the value to print
283    */

284   final public void println(float v)
285   {
286     String JavaDoc s = String.valueOf(v);
287     
288     write(s, 0, s.length());
289     println();
290   }
291   
292   
293   /**
294    * Prints a double followed by a newline.
295    *
296    * @param v the value to print
297    */

298   final public void println(double v)
299   {
300     String JavaDoc s = String.valueOf(v);
301     
302     write(s, 0, s.length());
303     
304     println();
305   }
306
307   /**
308    * Writes a character array followed by a newline.
309    */

310   final public void println(char []s)
311   {
312     write(s, 0, s.length);
313     println();
314   }
315
316   /**
317    * Writes a string followed by a newline.
318    */

319   final public void println(String JavaDoc s)
320   {
321     if (s == null)
322       write(_nullChars, 0, _nullChars.length);
323     else
324       write(s, 0, s.length());
325
326     println();
327   }
328   
329   /**
330    * Writes an object followed by a newline.
331    */

332   final public void println(Object JavaDoc v)
333   {
334     if (v == null)
335       write(_nullChars, 0, _nullChars.length);
336     else {
337       String JavaDoc s = v.toString();
338
339       write(s, 0, s.length());
340     }
341     
342     println();
343   }
344 }
345
Popular Tags