001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.desktop.util;
006:
007: import java.io.PrintWriter;
008: import java.io.Writer;
009: import java.io.OutputStream;
010: import java.io.IOException;
011: import java.io.BufferedWriter;
012: import java.io.OutputStreamWriter;
013: import java.io.InterruptedIOException;
014:
015: /**
016: * This class is taken from the JDK 1.2.2_11 reference
017: * implementation and modified to be unsynchronized, for
018: * performance reasons. Objects based on this class
019: * must not be used
020: * in a context where they are accessed by multiple
021: * threads.
022: */
023:
024: public class NSPrintWriter extends PrintWriter {
025:
026: /**
027: * The underlying character-output stream of this
028: * <code>PrintWriter</code>.
029: *
030: * @since 1.2
031: */
032: protected Writer out;
033:
034: private boolean autoFlush = false;
035: private boolean trouble = false;
036:
037: /**
038: * Line separator string. This is the value of the line.separator
039: * property at the moment that the stream was created.
040: */
041: private String lineSeparator;
042:
043: /**
044: * Create a new PrintWriter, without automatic line flushing.
045: *
046: * @param out A character-output stream
047: */
048: public NSPrintWriter(Writer out) {
049: this (out, false);
050: }
051:
052: /**
053: * Create a new PrintWriter.
054: *
055: * @param out A character-output stream
056: * @param autoFlush A boolean; if true, the println() methods will flush
057: * the output buffer
058: */
059: public NSPrintWriter(Writer out, boolean autoFlush) {
060: super (out);
061: this .out = out;
062: this .autoFlush = autoFlush;
063: lineSeparator = (String) java.security.AccessController
064: .doPrivileged(new sun.security.action.GetPropertyAction(
065: "line.separator"));
066: }
067:
068: /**
069: * Create a new PrintWriter, without automatic line flushing, from an
070: * existing OutputStream. This convenience constructor creates the
071: * necessary intermediate OutputStreamWriter, which will convert characters
072: * into bytes using the default character encoding.
073: *
074: * @param out An output stream
075: *
076: * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
077: */
078: public NSPrintWriter(OutputStream out) {
079: this (out, false);
080: }
081:
082: /**
083: * Create a new PrintWriter from an existing OutputStream. This
084: * convenience constructor creates the necessary intermediate
085: * OutputStreamWriter, which will convert characters into bytes using the
086: * default character encoding.
087: *
088: * @param out An output stream
089: * @param autoFlush A boolean; if true, the println() methods will flush
090: * the output buffer
091: *
092: * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
093: */
094: public NSPrintWriter(OutputStream out, boolean autoFlush) {
095: this (new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
096: }
097:
098: /** Check to make sure that the stream has not been closed */
099: private void ensureOpen() throws IOException {
100: if (out == null)
101: throw new IOException("Stream closed");
102: }
103:
104: /** Flush the stream. */
105: public void flush() {
106: try {
107: ensureOpen();
108: out.flush();
109: } catch (IOException x) {
110: trouble = true;
111: }
112: }
113:
114: /** Close the stream. */
115: public void close() {
116: try {
117: if (out == null)
118: return;
119: out.close();
120: out = null;
121: } catch (IOException x) {
122: trouble = true;
123: }
124: }
125:
126: /**
127: * Flush the stream and check its error state. Errors are cumulative;
128: * once the stream encounters an error, this routine will return true on
129: * all successive calls.
130: *
131: * @return True if the print stream has encountered an error, either on the
132: * underlying output stream or during a format conversion.
133: */
134: public boolean checkError() {
135: if (out != null)
136: flush();
137: return trouble;
138: }
139:
140: /** Indicate that an error has occurred. */
141: protected void setError() {
142: trouble = true;
143: }
144:
145: /*
146: * Exception-catching, synchronized output operations,
147: * which also implement the write() methods of Writer
148: */
149:
150: /** Write a single character. */
151: public void write(int c) {
152: try {
153: ensureOpen();
154: out.write(c);
155: } catch (InterruptedIOException x) {
156: Thread.currentThread().interrupt();
157: } catch (IOException x) {
158: trouble = true;
159: }
160: }
161:
162: /** Write a portion of an array of characters. */
163: public void write(char buf[], int off, int len) {
164: try {
165: ensureOpen();
166: out.write(buf, off, len);
167: } catch (InterruptedIOException x) {
168: Thread.currentThread().interrupt();
169: } catch (IOException x) {
170: trouble = true;
171: }
172: }
173:
174: /**
175: * Write an array of characters. This method cannot be inherited from the
176: * Writer class because it must suppress I/O exceptions.
177: */
178: public void write(char buf[]) {
179: write(buf, 0, buf.length);
180: }
181:
182: /** Write a portion of a string. */
183: public void write(String s, int off, int len) {
184: try {
185: ensureOpen();
186: out.write(s, off, len);
187: } catch (InterruptedIOException x) {
188: Thread.currentThread().interrupt();
189: } catch (IOException x) {
190: trouble = true;
191: }
192: }
193:
194: /**
195: * Write a string. This method cannot be inherited from the Writer class
196: * because it must suppress I/O exceptions.
197: */
198: public void write(String s) {
199: write(s, 0, s.length());
200: }
201:
202: private void newLine() {
203: try {
204: ensureOpen();
205: out.write(lineSeparator);
206: if (autoFlush)
207: out.flush();
208: } catch (InterruptedIOException x) {
209: Thread.currentThread().interrupt();
210: } catch (IOException x) {
211: trouble = true;
212: }
213: }
214:
215: /* Methods that do not terminate lines */
216:
217: /**
218: * Print a boolean value. The string produced by <code>{@link
219: * java.lang.String#valueOf(boolean)}</code> is translated into bytes
220: * according to the platform's default character encoding, and these bytes
221: * are written in exactly the manner of the <code>{@link
222: * #write(int)}</code> method.
223: *
224: * @param b The <code>boolean</code> to be printed
225: */
226: public void print(boolean b) {
227: write(b ? "true" : "false");
228: }
229:
230: /**
231: * Print a character. The character is translated into one or more bytes
232: * according to the platform's default character encoding, and these bytes
233: * are written in exactly the manner of the <code>{@link
234: * #write(int)}</code> method.
235: *
236: * @param c The <code>char</code> to be printed
237: */
238: public void print(char c) {
239: write(String.valueOf(c));
240: }
241:
242: /**
243: * Print an integer. The string produced by <code>{@link
244: * java.lang.String#valueOf(int)}</code> is translated into bytes according
245: * to the platform's default character encoding, and these bytes are
246: * written in exactly the manner of the <code>{@link #write(int)}</code>
247: * method.
248: *
249: * @param i The <code>int</code> to be printed
250: * @see java.lang.Integer#toString(int)
251: */
252: public void print(int i) {
253: write(String.valueOf(i));
254: }
255:
256: /**
257: * Print a long integer. The string produced by <code>{@link
258: * java.lang.String#valueOf(long)}</code> is translated into bytes
259: * according to the platform's default character encoding, and these bytes
260: * are written in exactly the manner of the <code>{@link #write(int)}</code>
261: * method.
262: *
263: * @param l The <code>long</code> to be printed
264: * @see java.lang.Long#toString(long)
265: */
266: public void print(long l) {
267: write(String.valueOf(l));
268: }
269:
270: /**
271: * Print a floating-point number. The string produced by <code>{@link
272: * java.lang.String#valueOf(float)}</code> is translated into bytes
273: * according to the platform's default character encoding, and these bytes
274: * are written in exactly the manner of the <code>{@link #write(int)}</code>
275: * method.
276: *
277: * @param f The <code>float</code> to be printed
278: * @see java.lang.Float#toString(float)
279: */
280: public void print(float f) {
281: write(String.valueOf(f));
282: }
283:
284: /**
285: * Print a double-precision floating-point number. The string produced by
286: * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
287: * bytes according to the platform's default character encoding, and these
288: * bytes are written in exactly the manner of the <code>{@link
289: * #write(int)}</code> method.
290: *
291: * @param d The <code>double</code> to be printed
292: * @see java.lang.Double#toString(double)
293: */
294: public void print(double d) {
295: write(String.valueOf(d));
296: }
297:
298: /**
299: * Print an array of characters. The characters are converted into bytes
300: * according to the platform's default character encoding, and these bytes
301: * are written in exactly the manner of the <code>{@link #write(int)}</code>
302: * method.
303: *
304: * @param s The array of chars to be printed
305: *
306: * @throws NullPointerException If <code>s</code> is <code>null</code>
307: */
308: public void print(char s[]) {
309: write(s);
310: }
311:
312: /**
313: * Print a string. If the argument is <code>null</code> then the string
314: * <code>"null"</code> is printed. Otherwise, the string's characters are
315: * converted into bytes according to the platform's default character
316: * encoding, and these bytes are written in exactly the manner of the
317: * <code>{@link #write(int)}</code> method.
318: *
319: * @param s The <code>String</code> to be printed
320: */
321: public void print(String s) {
322: if (s == null) {
323: s = "null";
324: }
325: write(s);
326: }
327:
328: /**
329: * Print an object. The string produced by the <code>{@link
330: * java.lang.String#valueOf(Object)}</code> method is translated into bytes
331: * according to the platform's default character encoding, and these bytes
332: * are written in exactly the manner of the <code>{@link #write(int)}</code>
333: * method.
334: *
335: * @param obj The <code>Object</code> to be printed
336: * @see java.lang.Object#toString()
337: */
338: public void print(Object obj) {
339: write(String.valueOf(obj));
340: }
341:
342: /* Methods that do terminate lines */
343:
344: /**
345: * Terminate the current line by writing the line separator string. The
346: * line separator string is defined by the system property
347: * <code>line.separator</code>, and is not necessarily a single newline
348: * character (<code>'\n'</code>).
349: */
350: public void println() {
351: newLine();
352: }
353:
354: /**
355: * Print a boolean value and then terminate the line. This method behaves
356: * as though it invokes <code>{@link #print(boolean)}</code> and then
357: * <code>{@link #println()}</code>.
358: *
359: * @param x the <code>boolean</code> value to be printed
360: */
361: public void println(boolean x) {
362: print(x);
363: println();
364: }
365:
366: /**
367: * Print a character and then terminate the line. This method behaves as
368: * though it invokes <code>{@link #print(char)}</code> and then <code>{@link
369: * #println()}</code>.
370: *
371: * @param x the <code>char</code> value to be printed
372: */
373: public void println(char x) {
374: print(x);
375: println();
376: }
377:
378: /**
379: * Print an integer and then terminate the line. This method behaves as
380: * though it invokes <code>{@link #print(int)}</code> and then <code>{@link
381: * #println()}</code>.
382: *
383: * @param x the <code>int</code> value to be printed
384: */
385: public void println(int x) {
386: print(x);
387: println();
388: }
389:
390: /**
391: * Print a long integer and then terminate the line. This method behaves
392: * as though it invokes <code>{@link #print(long)}</code> and then
393: * <code>{@link #println()}</code>.
394: *
395: * @param x the <code>long</code> value to be printed
396: */
397: public void println(long x) {
398: print(x);
399: println();
400: }
401:
402: /**
403: * Print a floating-point number and then terminate the line. This method
404: * behaves as though it invokes <code>{@link #print(float)}</code> and then
405: * <code>{@link #println()}</code>.
406: *
407: * @param x the <code>float</code> value to be printed
408: */
409: public void println(float x) {
410: print(x);
411: println();
412: }
413:
414: /**
415: * Print a double-precision floating-point number and then terminate the
416: * line. This method behaves as though it invokes <code>{@link
417: * #print(double)}</code> and then <code>{@link #println()}</code>.
418: *
419: * @param x the <code>double</code> value to be printed
420: */
421: public void println(double x) {
422: print(x);
423: println();
424: }
425:
426: /**
427: * Print an array of characters and then terminate the line. This method
428: * behaves as though it invokes <code>{@link #print(char[])}</code> and then
429: * <code>{@link #println()}</code>.
430: *
431: * @param x the array of <code>char</code> values to be printed
432: */
433: public void println(char x[]) {
434: print(x);
435: println();
436: }
437:
438: /**
439: * Print a String and then terminate the line. This method behaves as
440: * though it invokes <code>{@link #print(String)}</code> and then
441: * <code>{@link #println()}</code>.
442: *
443: * @param x the <code>String</code> value to be printed
444: */
445: public void println(String x) {
446: print(x);
447: println();
448: }
449:
450: /**
451: * Print an Object and then terminate the line. This method behaves as
452: * though it invokes <code>{@link #print(Object)}</code> and then
453: * <code>{@link #println()}</code>.
454: *
455: * @param x the <code>Object</code> value to be printed
456: */
457: public void println(Object x) {
458: print(x);
459: println();
460: }
461:
462: }
|