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 writes to a specific WriteStream.
040: */
041: public class StreamPrintWriter extends PrintWriter implements
042: FlushBuffer, EnclosedWriteStream {
043: private final static Logger log = Logger
044: .getLogger(PrintWriterImpl.class.getName());
045:
046: private final static char[] _nullChars = "null".toCharArray();
047: private final static char[] _newline = "\n".toCharArray();
048:
049: private final static Writer _dummyWriter = new StringWriter();
050:
051: private final WriteStream _out;
052:
053: /**
054: * Creates a new PrintWriterImpl
055: */
056: public StreamPrintWriter(WriteStream out) {
057: super ((Writer) _dummyWriter);
058:
059: _out = out;
060: }
061:
062: /**
063: * Writes a character.
064: */
065: final public void write(int ch) {
066: try {
067: _out.print((char) ch);
068: } catch (IOException e) {
069: log.log(Level.FINE, e.toString(), e);
070: }
071: }
072:
073: /**
074: * Writes a character.
075: */
076: final public void write(char[] buf, int offset, int length) {
077: try {
078: _out.print(buf, offset, length);
079: } catch (IOException e) {
080: log.log(Level.FINE, e.toString(), e);
081: }
082: }
083:
084: /**
085: * Writes a character buffer.
086: */
087: final public void write(char[] buf) {
088: try {
089: _out.print(buf, 0, buf.length);
090: } catch (IOException e) {
091: log.log(Level.FINE, e.toString(), e);
092: }
093: }
094:
095: /**
096: * Writes a string
097: */
098: final public void write(String v) {
099: try {
100: _out.print(v);
101: } catch (IOException e) {
102: log.log(Level.FINE, e.toString(), e);
103: }
104: }
105:
106: /**
107: * Writes a string
108: */
109: final public void write(String v, int offset, int length) {
110: try {
111: _out.print(v, offset, length);
112: } catch (IOException e) {
113: log.log(Level.FINE, e.toString(), e);
114: }
115: }
116:
117: /**
118: * Prints a character.
119: */
120: final public void print(char ch) {
121: try {
122: _out.print(ch);
123: } catch (IOException e) {
124: log.log(Level.FINE, e.toString(), e);
125: }
126: }
127:
128: /**
129: * Prints an integer.
130: */
131: final public void print(int v) {
132: try {
133: _out.print(v);
134: } catch (IOException e) {
135: log.log(Level.FINE, e.toString(), e);
136: }
137: }
138:
139: /**
140: * Prints a long.
141: */
142: final public void print(long v) {
143: try {
144: _out.print(v);
145: } catch (IOException e) {
146: log.log(Level.FINE, e.toString(), e);
147: }
148: }
149:
150: /**
151: * Prints a double followed by a newline.
152: *
153: * @param v the value to print
154: */
155: final public void print(float v) {
156: try {
157: _out.print(v);
158: } catch (IOException e) {
159: log.log(Level.FINE, e.toString(), e);
160: }
161: }
162:
163: /**
164: * Prints a double followed by a newline.
165: *
166: * @param v the value to print
167: */
168: final public void print(double v) {
169: try {
170: _out.print(v);
171: } catch (IOException e) {
172: log.log(Level.FINE, e.toString(), e);
173: }
174: }
175:
176: /**
177: * Prints a character array
178: */
179: final public void print(char[] v) {
180: try {
181: _out.print(v);
182: } catch (IOException e) {
183: log.log(Level.FINE, e.toString(), e);
184: }
185: }
186:
187: /**
188: * Prints a string.
189: */
190: final public void print(String v) {
191: try {
192: _out.print(v);
193: } catch (IOException e) {
194: log.log(Level.FINE, e.toString(), e);
195: }
196: }
197:
198: /**
199: * Prints the value of the object.
200: */
201: final public void print(Object v) {
202: try {
203: _out.print(v);
204: } catch (IOException e) {
205: log.log(Level.FINE, e.toString(), e);
206: }
207: }
208:
209: /**
210: * Prints the newline.
211: */
212: final public void println() {
213: try {
214: _out.println();
215: } catch (IOException e) {
216: log.log(Level.FINE, e.toString(), e);
217: }
218: }
219:
220: /**
221: * Prints the boolean followed by a newline.
222: *
223: * @param v the value to print
224: */
225: final public void println(boolean v) {
226: try {
227: _out.println(v);
228: } catch (IOException e) {
229: log.log(Level.FINE, e.toString(), e);
230: }
231: }
232:
233: /**
234: * Prints a character followed by a newline.
235: *
236: * @param v the value to print
237: */
238: final public void println(char v) {
239: try {
240: _out.println(v);
241: } catch (IOException e) {
242: log.log(Level.FINE, e.toString(), e);
243: }
244: }
245:
246: /**
247: * Prints an integer followed by a newline.
248: *
249: * @param v the value to print
250: */
251: final public void println(int v) {
252: try {
253: _out.println(v);
254: } catch (IOException e) {
255: log.log(Level.FINE, e.toString(), e);
256: }
257: }
258:
259: /**
260: * Prints a long followed by a newline.
261: *
262: * @param v the value to print
263: */
264: final public void println(long v) {
265: try {
266: _out.println(v);
267: } catch (IOException e) {
268: log.log(Level.FINE, e.toString(), e);
269: }
270: }
271:
272: /**
273: * Prints a float followed by a newline.
274: *
275: * @param v the value to print
276: */
277: final public void println(float v) {
278: try {
279: _out.println(v);
280: } catch (IOException e) {
281: log.log(Level.FINE, e.toString(), e);
282: }
283: }
284:
285: /**
286: * Prints a double followed by a newline.
287: *
288: * @param v the value to print
289: */
290: final public void println(double v) {
291: try {
292: _out.println(v);
293: } catch (IOException e) {
294: log.log(Level.FINE, e.toString(), e);
295: }
296: }
297:
298: /**
299: * Writes a character array followed by a newline.
300: */
301: final public void println(char[] v) {
302: try {
303: _out.println(v);
304: } catch (IOException e) {
305: log.log(Level.FINE, e.toString(), e);
306: }
307: }
308:
309: /**
310: * Writes a string followed by a newline.
311: */
312: final public void println(String v) {
313: try {
314: _out.println(v);
315: } catch (IOException e) {
316: log.log(Level.FINE, e.toString(), e);
317: }
318: }
319:
320: /**
321: * Writes an object followed by a newline.
322: */
323: final public void println(Object v) {
324: try {
325: _out.println(v);
326: } catch (IOException e) {
327: log.log(Level.FINE, e.toString(), e);
328: }
329: }
330:
331: /**
332: * Flushes the writer.
333: */
334: public void flush() {
335: try {
336: _out.flush();
337: } catch (IOException e) {
338: log.log(Level.FINE, e.toString(), e);
339: }
340: }
341:
342: /**
343: * Flushes the writer.
344: */
345: public void flushBuffer() {
346: try {
347: _out.flushBuffer();
348: } catch (IOException e) {
349: log.log(Level.FINE, e.toString(), e);
350: }
351: }
352:
353: public WriteStream getWriteStream() {
354: return _out;
355: }
356:
357: public void close() {
358: }
359: }
|