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.IOException;
032: import java.io.PrintWriter;
033: import java.io.StringWriter;
034: import java.io.Writer;
035: import java.util.logging.Level;
036: import java.util.logging.Logger;
037:
038: /**
039: * A print writer which allows for a changing writer.
040: */
041: public class PrintWriterImpl extends PrintWriter implements FlushBuffer {
042: private final static Logger log = Logger
043: .getLogger(PrintWriterImpl.class.getName());
044:
045: private final static char[] _nullChars = "null".toCharArray();
046: private final static char[] _newline = "\n".toCharArray();
047:
048: private final static Writer _dummyWriter = new StringWriter();
049:
050: private final char[] _tempCharBuffer = new char[64];
051:
052: /**
053: * Creates a new PrintWriterImpl
054: */
055: public PrintWriterImpl() {
056: super ((Writer) _dummyWriter);
057: }
058:
059: /**
060: * Creates a new PrintWriterImpl
061: */
062: public PrintWriterImpl(Writer out) {
063: super (out);
064: }
065:
066: /**
067: * Sets the underlying writer.
068: */
069: public void setWriter(Writer out) {
070: this .out = out;
071: }
072:
073: /**
074: * Writes a character.
075: */
076: final public void write(char ch) {
077: Writer out = this .out;
078: if (out == null)
079: return;
080:
081: try {
082: out.write(ch);
083: } catch (IOException e) {
084: log.log(Level.FINE, e.toString(), e);
085: }
086: }
087:
088: /**
089: * Writes a character.
090: */
091: final public void write(char[] buf, int offset, int length) {
092: Writer out = this .out;
093: if (out == null)
094: return;
095:
096: try {
097: out.write(buf, offset, length);
098: } catch (IOException e) {
099: log.log(Level.FINE, e.toString(), e);
100: }
101: }
102:
103: /**
104: * Writes a character buffer.
105: */
106: final public void write(char[] buf) {
107: Writer out = this .out;
108: if (out == null)
109: return;
110:
111: try {
112: out.write(buf, 0, buf.length);
113: } catch (IOException e) {
114: log.log(Level.FINE, e.toString(), e);
115: }
116: }
117:
118: /**
119: * Prints a character.
120: */
121: final public void print(char ch) {
122: Writer out = this .out;
123: if (out == null)
124: return;
125:
126: try {
127: out.write(ch);
128: } catch (IOException e) {
129: log.log(Level.FINE, e.toString(), e);
130: }
131: }
132:
133: /**
134: * Prints an integer.
135: */
136: final public void print(int i) {
137: Writer out = this .out;
138: if (out == null)
139: return;
140:
141: if (i == 0x80000000) {
142: print("-2147483648");
143: return;
144: }
145:
146: try {
147: if (i < 0) {
148: out.write('-');
149: i = -i;
150: } else if (i < 9) {
151: out.write('0' + i);
152: return;
153: }
154:
155: int length = 0;
156: int exp = 10;
157:
158: if (i >= 1000000000)
159: length = 9;
160: else {
161: for (; i >= exp; length++)
162: exp = 10 * exp;
163: }
164:
165: int j = 31;
166:
167: while (i > 0) {
168: _tempCharBuffer[--j] = (char) ((i % 10) + '0');
169: i /= 10;
170: }
171:
172: out.write(_tempCharBuffer, j, 31 - j);
173: } catch (IOException e) {
174: log.log(Level.FINE, e.toString(), e);
175: }
176: }
177:
178: /**
179: * Prints a long.
180: */
181: final public void print(long v) {
182: Writer out = this .out;
183: if (out == null)
184: return;
185:
186: if (v == 0x8000000000000000L) {
187: print("-9223372036854775808");
188: return;
189: }
190:
191: try {
192: if (v < 0) {
193: out.write('-');
194: v = -v;
195: } else if (v == 0) {
196: out.write('0');
197: return;
198: }
199:
200: int j = 31;
201:
202: while (v > 0) {
203: _tempCharBuffer[--j] = (char) ((v % 10) + '0');
204: v /= 10;
205: }
206:
207: out.write(_tempCharBuffer, j, 31 - j);
208: } catch (IOException e) {
209: log.log(Level.FINE, e.toString(), e);
210: }
211: }
212:
213: /**
214: * Prints a double followed by a newline.
215: *
216: * @param v the value to print
217: */
218: final public void print(float v) {
219: Writer out = this .out;
220: if (out == null)
221: return;
222:
223: try {
224: String s = String.valueOf(v);
225: out.write(s, 0, s.length());
226: } catch (IOException e) {
227: log.log(Level.FINE, e.toString(), e);
228: }
229: }
230:
231: /**
232: * Prints a double followed by a newline.
233: *
234: * @param v the value to print
235: */
236: final public void print(double v) {
237: Writer out = this .out;
238: if (out == null)
239: return;
240:
241: try {
242: String s = String.valueOf(v);
243: out.write(s, 0, s.length());
244: } catch (IOException e) {
245: log.log(Level.FINE, e.toString(), e);
246: }
247: }
248:
249: /**
250: * Prints a character array
251: */
252: final public void print(char[] s) {
253: Writer out = this .out;
254: if (out == null)
255: return;
256:
257: try {
258: out.write(s, 0, s.length);
259: } catch (IOException e) {
260: log.log(Level.FINE, e.toString(), e);
261: }
262: }
263:
264: /**
265: * Prints a string.
266: */
267: final public void print(String s) {
268: Writer out = this .out;
269: if (out == null)
270: return;
271:
272: try {
273: if (s == null)
274: out.write(_nullChars, 0, _nullChars.length);
275: else
276: out.write(s, 0, s.length());
277: } catch (IOException e) {
278: log.log(Level.FINE, e.toString(), e);
279: }
280: }
281:
282: /**
283: * Prints the value of the object.
284: */
285: final public void print(Object v) {
286: Writer out = this .out;
287: if (out == null)
288: return;
289:
290: try {
291: if (v == null)
292: out.write(_nullChars, 0, _nullChars.length);
293: else {
294: String s = v.toString();
295:
296: out.write(s, 0, s.length());
297: }
298: } catch (IOException e) {
299: log.log(Level.FINE, e.toString(), e);
300: }
301: }
302:
303: /**
304: * Prints the newline.
305: */
306: final public void println() {
307: Writer out = this .out;
308: if (out == null)
309: return;
310:
311: try {
312: out.write(_newline, 0, _newline.length);
313: } catch (IOException e) {
314: log.log(Level.FINE, e.toString(), e);
315: }
316: }
317:
318: /**
319: * Prints the boolean followed by a newline.
320: *
321: * @param v the value to print
322: */
323: final public void println(boolean v) {
324: Writer out = this .out;
325: if (out == null)
326: return;
327:
328: print(v);
329:
330: try {
331: out.write(_newline, 0, _newline.length);
332: } catch (IOException e) {
333: log.log(Level.FINE, e.toString(), e);
334: }
335: }
336:
337: /**
338: * Prints a character followed by a newline.
339: *
340: * @param v the value to print
341: */
342: final public void println(char v) {
343: Writer out = this .out;
344: if (out == null)
345: return;
346:
347: try {
348: out.write(v);
349: out.write(_newline, 0, _newline.length);
350: } catch (IOException e) {
351: log.log(Level.FINE, e.toString(), e);
352: }
353: }
354:
355: /**
356: * Prints an integer followed by a newline.
357: *
358: * @param v the value to print
359: */
360: final public void println(int v) {
361: Writer out = this .out;
362: if (out == null)
363: return;
364:
365: print(v);
366:
367: try {
368: out.write(_newline, 0, _newline.length);
369: } catch (IOException e) {
370: log.log(Level.FINE, e.toString(), e);
371: }
372: }
373:
374: /**
375: * Prints a long followed by a newline.
376: *
377: * @param v the value to print
378: */
379: final public void println(long v) {
380: Writer out = this .out;
381: if (out == null)
382: return;
383:
384: print(v);
385:
386: try {
387: out.write(_newline, 0, _newline.length);
388: } catch (IOException e) {
389: log.log(Level.FINE, e.toString(), e);
390: }
391: }
392:
393: /**
394: * Prints a float followed by a newline.
395: *
396: * @param v the value to print
397: */
398: final public void println(float v) {
399: Writer out = this .out;
400: if (out == null)
401: return;
402:
403: String s = String.valueOf(v);
404:
405: try {
406: out.write(s, 0, s.length());
407: out.write(_newline, 0, _newline.length);
408: } catch (IOException e) {
409: log.log(Level.FINE, e.toString(), e);
410: }
411: }
412:
413: /**
414: * Prints a double followed by a newline.
415: *
416: * @param v the value to print
417: */
418: final public void println(double v) {
419: Writer out = this .out;
420: if (out == null)
421: return;
422:
423: print(v);
424:
425: try {
426: out.write(_newline, 0, _newline.length);
427: } catch (IOException e) {
428: log.log(Level.FINE, e.toString(), e);
429: }
430: }
431:
432: /**
433: * Writes a character array followed by a newline.
434: */
435: final public void println(char[] s) {
436: Writer out = this .out;
437: if (out == null)
438: return;
439:
440: try {
441: out.write(s, 0, s.length);
442: out.write(_newline, 0, _newline.length);
443: } catch (IOException e) {
444: log.log(Level.FINE, e.toString(), e);
445: }
446: }
447:
448: /**
449: * Writes a string followed by a newline.
450: */
451: final public void println(String s) {
452: Writer out = this .out;
453: if (out == null)
454: return;
455:
456: try {
457: if (s == null)
458: out.write(_nullChars, 0, _nullChars.length);
459: else
460: out.write(s, 0, s.length());
461:
462: out.write(_newline, 0, _newline.length);
463: } catch (IOException e) {
464: log.log(Level.FINE, e.toString(), e);
465: }
466: }
467:
468: /**
469: * Writes an object followed by a newline.
470: */
471: final public void println(Object v) {
472: Writer out = this .out;
473: if (out == null)
474: return;
475:
476: try {
477: if (v == null)
478: out.write(_nullChars, 0, _nullChars.length);
479: else {
480: String s = v.toString();
481:
482: out.write(s, 0, s.length());
483: }
484:
485: out.write(_newline, 0, _newline.length);
486: } catch (IOException e) {
487: log.log(Level.FINE, e.toString(), e);
488: }
489: }
490:
491: /**
492: * Flushes the writer.
493: */
494: public void flush() {
495: Writer out = this .out;
496: if (out == null)
497: return;
498:
499: try {
500: out.flush();
501: } catch (IOException e) {
502: log.log(Level.FINE, e.toString(), e);
503: }
504: }
505:
506: /**
507: * Flushes the writer.
508: */
509: public void flushBuffer() {
510: Writer out = this .out;
511: if (out == null)
512: return;
513:
514: try {
515: if (out instanceof FlushBuffer)
516: ((FlushBuffer) out).flushBuffer();
517: } catch (IOException e) {
518: log.log(Level.FINE, e.toString(), e);
519: }
520: }
521:
522: public void close() {
523: this.out = null;
524: }
525: }
|