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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.vfs;
030:
031: import java.io.PrintWriter;
032: import java.io.StringWriter;
033: import java.io.Writer;
034:
035: /**
036: * An abstract print writer.
037: */
038: public abstract class AbstractPrintWriter extends PrintWriter {
039: private static final char[] _trueChars = "true".toCharArray();
040: private static final char[] _falseChars = "false".toCharArray();
041: private static final char[] _nullChars = "null".toCharArray();
042:
043: private static final Writer _dummyWriter = new StringWriter();
044:
045: private final char[] _tempCharBuffer = new char[64];
046:
047: /**
048: * Creates the print writer.
049: */
050: protected AbstractPrintWriter() {
051: super (_dummyWriter);
052: }
053:
054: /**
055: * Writes a character array to the writer.
056: *
057: * @param buf the buffer to write.
058: * @param off the offset into the buffer
059: * @param len the number of characters to write
060: */
061: abstract public void write(char[] buf, int offset, int length);
062:
063: /**
064: * Writes a character to the output.
065: *
066: * @param buf the buffer to write.
067: */
068: abstract public void write(int ch);
069:
070: /**
071: * Writes a subsection of a string to the output.
072: */
073: abstract public void write(String s, int off, int len);
074:
075: /**
076: * Writes a char buffer to the output.
077: *
078: * @param buf the buffer to write.
079: */
080: final public void write(char[] buf) {
081: write(buf, 0, buf.length);
082: }
083:
084: /**
085: * Writes a string to the output.
086: */
087: final public void write(String s) {
088: write(s, 0, s.length());
089: }
090:
091: /**
092: * Writes the newline character.
093: */
094: public void newLine() {
095: write('\n');
096: }
097:
098: /**
099: * Prints a boolean.
100: */
101: final public void print(boolean b) {
102: write(b ? _trueChars : _falseChars);
103: }
104:
105: /**
106: * Prints a character.
107: */
108: public void print(char ch) {
109: write(ch);
110: }
111:
112: /**
113: * Prints an integer value.
114: */
115: public void print(int i) {
116: if (i == 0x80000000) {
117: print("-2147483648");
118: return;
119: }
120:
121: if (i < 0) {
122: write('-');
123: i = -i;
124: } else if (i < 9) {
125: write('0' + i);
126: return;
127: }
128:
129: int length = 0;
130: int exp = 10;
131:
132: if (i >= 1000000000)
133: length = 9;
134: else {
135: for (; i >= exp; length++)
136: exp = 10 * exp;
137: }
138:
139: int j = 31;
140:
141: while (i > 0) {
142: _tempCharBuffer[--j] = (char) ((i % 10) + '0');
143: i /= 10;
144: }
145:
146: write(_tempCharBuffer, j, 31 - j);
147: }
148:
149: /**
150: * Prints a long value.
151: */
152: public void print(long v) {
153: if (v == 0x8000000000000000L) {
154: print("-9223372036854775808");
155: return;
156: }
157:
158: if (v < 0) {
159: write('-');
160: v = -v;
161: } else if (v == 0) {
162: write('0');
163: return;
164: }
165:
166: int j = 31;
167:
168: while (v > 0) {
169: _tempCharBuffer[--j] = (char) ((v % 10) + '0');
170: v /= 10;
171: }
172:
173: write(_tempCharBuffer, j, 31 - j);
174: }
175:
176: final public void print(float f) {
177: write(String.valueOf(f));
178: }
179:
180: final public void print(double d) {
181: write(String.valueOf(d));
182: }
183:
184: /**
185: * Prints a character array
186: */
187: final public void print(char[] s) {
188: write(s, 0, s.length);
189: }
190:
191: /**
192: * Prints a string.
193: */
194: final public void print(String s) {
195: if (s == null)
196: write(_nullChars, 0, _nullChars.length);
197: else
198: write(s, 0, s.length());
199: }
200:
201: /**
202: * Prints the value of the object.
203: */
204: final public void print(Object v) {
205: if (v == null)
206: write(_nullChars, 0, _nullChars.length);
207: else {
208: String s = v.toString();
209:
210: write(s, 0, s.length());
211: }
212: }
213:
214: /**
215: * Prints the newline.
216: */
217: public void println() {
218: write('\n');
219: }
220:
221: /**
222: * Prints the boolean followed by a newline.
223: *
224: * @param v the value to print
225: */
226: final public void println(boolean v) {
227: print(v);
228: println();
229: }
230:
231: /**
232: * Prints a character followed by a newline.
233: *
234: * @param v the value to print
235: */
236: final public void println(char v) {
237: print(v);
238: println();
239: }
240:
241: /**
242: * Prints an integer followed by a newline.
243: *
244: * @param v the value to print
245: */
246: final public void println(int v) {
247: print(v);
248: println();
249: }
250:
251: /**
252: * Prints a long followed by a newline.
253: *
254: * @param v the value to print
255: */
256: final public void println(long v) {
257: print(v);
258: println();
259: }
260:
261: /**
262: * Prints a float followed by a newline.
263: *
264: * @param v the value to print
265: */
266: final public void println(float v) {
267: String s = String.valueOf(v);
268:
269: write(s, 0, s.length());
270: println();
271: }
272:
273: /**
274: * Prints a double followed by a newline.
275: *
276: * @param v the value to print
277: */
278: final public void println(double v) {
279: String s = String.valueOf(v);
280:
281: write(s, 0, s.length());
282:
283: println();
284: }
285:
286: /**
287: * Writes a character array followed by a newline.
288: */
289: final public void println(char[] s) {
290: write(s, 0, s.length);
291: println();
292: }
293:
294: /**
295: * Writes a string followed by a newline.
296: */
297: final public void println(String s) {
298: if (s == null)
299: write(_nullChars, 0, _nullChars.length);
300: else
301: write(s, 0, s.length());
302:
303: println();
304: }
305:
306: /**
307: * Writes an object followed by a newline.
308: */
309: final public void println(Object v) {
310: if (v == null)
311: write(_nullChars, 0, _nullChars.length);
312: else {
313: String s = v.toString();
314:
315: write(s, 0, s.length());
316: }
317:
318: println();
319: }
320: }
|