1 /* PrintStream.java -- OutputStream for printing output
2 Copyright (C) 1998, 1999, 2001, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.util
.Formatter
;
42 import java
.util
.Locale
;
44 import gnu
.gcj
.convert
.UnicodeToBytes
;
46 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
47 * "The Java Language Specification", ISBN 0-201-63451-1
48 * Status: Believed complete and correct to 1.3
52 * This class prints Java primitive values and object to a stream as
53 * text. None of the methods in this class throw an exception. However,
54 * errors can be detected by calling the <code>checkError()</code> method.
55 * Additionally, this stream can be designated as "autoflush" when
56 * created so that any writes are automatically flushed to the underlying
57 * output sink when the current line is terminated.
59 * This class converts char's into byte's using the system default encoding.
61 * @author Aaron M. Renn (arenn@urbanophile.com)
62 * @author Tom Tromey (tromey@cygnus.com)
64 public class PrintStream
extends FilterOutputStream
implements Appendable
66 /* Notice the implementation is quite similar to OutputStreamWriter.
67 * This leads to some minor duplication, because neither inherits
68 * from the other, and we want to maximize performance. */
70 // Line separator string.
71 private static final char[] line_separator
72 = System
.getProperty("line.separator").toCharArray();
74 UnicodeToBytes converter
;
76 // Work buffer of characters for converter.
77 char[] work
= new char[100];
78 // Work buffer of bytes where we temporarily keep converter output.
79 byte[] work_bytes
= new byte[100];
82 * This boolean indicates whether or not an error has ever occurred
85 private boolean error_occurred
= false;
88 * This is <code>true</code> if auto-flush is enabled,
89 * <code>false</code> otherwise
91 private boolean auto_flush
;
94 * This method intializes a new <code>PrintStream</code> object to write
95 * to the specified output sink.
97 * @param out The <code>OutputStream</code> to write to.
99 public PrintStream (OutputStream out
)
105 * This method intializes a new <code>PrintStream</code> object to write
106 * to the specified output sink. This constructor also allows "auto-flush"
107 * functionality to be specified where the stream will be flushed after
108 * every <code>print</code> or <code>println</code> call, when the
109 * <code>write</code> methods with array arguments are called, or when a
110 * single new-line character is written.
113 * @param out The <code>OutputStream</code> to write to.
114 * @param auto_flush <code>true</code> to flush the stream after every
115 * line, <code>false</code> otherwise
117 public PrintStream (OutputStream out
, boolean auto_flush
)
121 converter
= UnicodeToBytes
.getDefaultEncoder();
122 this.auto_flush
= auto_flush
;
126 * This method initializes a new <code>PrintStream</code> object to write
127 * to the specified output File. Doesn't autoflush.
129 * @param file The <code>File</code> to write to.
130 * @throws FileNotFoundException if an error occurs while opening the file.
134 public PrintStream (File file
)
135 throws FileNotFoundException
137 this (new FileOutputStream(file
), false);
141 * This method initializes a new <code>PrintStream</code> object to write
142 * to the specified output File. Doesn't autoflush.
144 * @param file The <code>File</code> to write to.
145 * @param encoding The name of the character encoding to use for this
147 * @throws FileNotFoundException If an error occurs while opening the file.
148 * @throws UnsupportedEncodingException If the charset specified by
149 * <code>encoding</code> is invalid.
153 public PrintStream (File file
, String encoding
)
154 throws FileNotFoundException
,UnsupportedEncodingException
156 this (new FileOutputStream(file
), false, encoding
);
160 * This method initializes a new <code>PrintStream</code> object to write
161 * to the specified output File. Doesn't autoflush.
163 * @param fileName The name of the <code>File</code> to write to.
164 * @throws FileNotFoundException if an error occurs while opening the file,
168 public PrintStream (String fileName
)
169 throws FileNotFoundException
171 this (new FileOutputStream(new File(fileName
)), false);
175 * This method initializes a new <code>PrintStream</code> object to write
176 * to the specified output File. Doesn't autoflush.
178 * @param fileName The name of the <code>File</code> to write to.
179 * @param encoding The name of the character encoding to use for this
181 * @throws FileNotFoundException if an error occurs while opening the file.
182 * @throws UnsupportedEncodingException If the charset specified by
183 * <code>encoding</code> is invalid.
187 public PrintStream (String fileName
, String encoding
)
188 throws FileNotFoundException
,UnsupportedEncodingException
190 this (new FileOutputStream(new File(fileName
)), false, encoding
);
194 * This method intializes a new <code>PrintStream</code> object to write
195 * to the specified output sink. This constructor also allows "auto-flush"
196 * functionality to be specified where the stream will be flushed after
197 * every <code>print</code> or <code>println</code> call, when the
198 * <code>write</code> methods with array arguments are called, or when a
199 * single new-line character is written.
202 * @param out The <code>OutputStream</code> to write to.
203 * @param auto_flush <code>true</code> to flush the stream after every
204 * line, <code>false</code> otherwise
205 * @param encoding The name of the character encoding to use for this
208 public PrintStream (OutputStream out
, boolean auto_flush
, String encoding
)
209 throws UnsupportedEncodingException
213 converter
= UnicodeToBytes
.getEncoder (encoding
);
214 this.auto_flush
= auto_flush
;
218 * This method checks to see if an error has occurred on this stream. Note
219 * that once an error has occurred, this method will continue to report
220 * <code>true</code> forever for this stream. Before checking for an
221 * error condition, this method flushes the stream.
223 * @return <code>true</code> if an error has occurred,
224 * <code>false</code> otherwise
226 public boolean checkError ()
229 return error_occurred
;
233 * This method can be called by subclasses to indicate that an error
234 * has occurred and should be reported by <code>checkError</code>.
236 protected void setError ()
238 error_occurred
= true;
242 * This method closes this stream and all underlying streams.
248 converter
.setFinished();
249 writeChars(new char[0], 0, 0);
253 catch (InterruptedIOException iioe
)
255 Thread
.currentThread().interrupt();
257 catch (IOException e
)
264 * This method flushes any buffered bytes to the underlying stream and
265 * then flushes that stream as well.
273 catch (InterruptedIOException iioe
)
275 Thread
.currentThread().interrupt();
277 catch (IOException e
)
283 private synchronized void print (String str
, boolean println
)
287 writeChars(str
, 0, str
.length());
289 writeChars(line_separator
, 0, line_separator
.length
);
293 catch (InterruptedIOException iioe
)
295 Thread
.currentThread().interrupt();
297 catch (IOException e
)
303 private synchronized void print (char[] chars
, int pos
, int len
,
308 writeChars(chars
, pos
, len
);
310 writeChars(line_separator
, 0, line_separator
.length
);
314 catch (InterruptedIOException iioe
)
316 Thread
.currentThread().interrupt();
318 catch (IOException e
)
324 private void writeChars(char[] buf
, int offset
, int count
)
329 converter
.setOutput(work_bytes
, 0);
330 int converted
= converter
.write(buf
, offset
, count
);
333 out
.write(work_bytes
, 0, converter
.count
);
335 while (count
> 0 || converter
.havePendingBytes());
338 private void writeChars(String str
, int offset
, int count
)
343 converter
.setOutput(work_bytes
, 0);
344 int converted
= converter
.write(str
, offset
, count
, work
);
347 out
.write(work_bytes
, 0, converter
.count
);
349 while (count
> 0 || converter
.havePendingBytes());
353 * This methods prints a boolean value to the stream. <code>true</code>
354 * values are printed as "true" and <code>false</code> values are printed
357 * @param bool The <code>boolean</code> value to print
359 public void print (boolean bool
)
361 print(String
.valueOf(bool
), false);
365 * This method prints an integer to the stream. The value printed is
366 * determined using the <code>String.valueOf()</code> method.
368 * @param inum The <code>int</code> value to be printed
370 public void print (int inum
)
372 print(String
.valueOf(inum
), false);
376 * This method prints a long to the stream. The value printed is
377 * determined using the <code>String.valueOf()</code> method.
379 * @param lnum The <code>long</code> value to be printed
381 public void print (long lnum
)
383 print(String
.valueOf(lnum
), false);
387 * This method prints a float to the stream. The value printed is
388 * determined using the <code>String.valueOf()</code> method.
390 * @param fnum The <code>float</code> value to be printed
392 public void print (float fnum
)
394 print(String
.valueOf(fnum
), false);
398 * This method prints a double to the stream. The value printed is
399 * determined using the <code>String.valueOf()</code> method.
401 * @param dnum The <code>double</code> value to be printed
403 public void print (double dnum
)
405 print(String
.valueOf(dnum
), false);
409 * This method prints an <code>Object</code> to the stream. The actual
410 * value printed is determined by calling the <code>String.valueOf()</code>
413 * @param obj The <code>Object</code> to print.
415 public void print (Object obj
)
417 print(obj
== null ?
"null" : obj
.toString(), false);
421 * This method prints a <code>String</code> to the stream. The actual
422 * value printed depends on the system default encoding.
424 * @param str The <code>String</code> to print.
426 public void print (String str
)
428 print(str
== null ?
"null" : str
, false);
432 * This method prints a char to the stream. The actual value printed is
433 * determined by the character encoding in use.
435 * @param ch The <code>char</code> value to be printed
437 public synchronized void print (char ch
)
440 print(work
, 0, 1, false);
444 * This method prints an array of characters to the stream. The actual
445 * value printed depends on the system default encoding.
447 * @param charArray The array of characters to print.
449 public void print (char[] charArray
)
451 print(charArray
, 0, charArray
.length
, false);
455 * This method prints a line separator sequence to the stream. The value
456 * printed is determined by the system property <xmp>line.separator</xmp>
457 * and is not necessarily the Unix '\n' newline character.
459 public void println ()
461 print(line_separator
, 0, line_separator
.length
, false);
465 * This methods prints a boolean value to the stream. <code>true</code>
466 * values are printed as "true" and <code>false</code> values are printed
469 * This method prints a line termination sequence after printing the value.
471 * @param bool The <code>boolean</code> value to print
473 public void println (boolean bool
)
475 print(String
.valueOf(bool
), true);
479 * This method prints an integer to the stream. The value printed is
480 * determined using the <code>String.valueOf()</code> method.
482 * This method prints a line termination sequence after printing the value.
484 * @param inum The <code>int</code> value to be printed
486 public void println (int inum
)
488 print(String
.valueOf(inum
), true);
492 * This method prints a long to the stream. The value printed is
493 * determined using the <code>String.valueOf()</code> method.
495 * This method prints a line termination sequence after printing the value.
497 * @param lnum The <code>long</code> value to be printed
499 public void println (long lnum
)
501 print(String
.valueOf(lnum
), true);
505 * This method prints a float to the stream. The value printed is
506 * determined using the <code>String.valueOf()</code> method.
508 * This method prints a line termination sequence after printing the value.
510 * @param fnum The <code>float</code> value to be printed
512 public void println (float fnum
)
514 print(String
.valueOf(fnum
), true);
518 * This method prints a double to the stream. The value printed is
519 * determined using the <code>String.valueOf()</code> method.
521 * This method prints a line termination sequence after printing the value.
523 * @param dnum The <code>double</code> value to be printed
525 public void println (double dnum
)
527 print(String
.valueOf(dnum
), true);
531 * This method prints an <code>Object</code> to the stream. The actual
532 * value printed is determined by calling the <code>String.valueOf()</code>
535 * This method prints a line termination sequence after printing the value.
537 * @param obj The <code>Object</code> to print.
539 public void println (Object obj
)
541 print(obj
== null ?
"null" : obj
.toString(), true);
545 * This method prints a <code>String</code> to the stream. The actual
546 * value printed depends on the system default encoding.
548 * This method prints a line termination sequence after printing the value.
550 * @param str The <code>String</code> to print.
552 public void println (String str
)
554 print (str
== null ?
"null" : str
, true);
558 * This method prints a char to the stream. The actual value printed is
559 * determined by the character encoding in use.
561 * This method prints a line termination sequence after printing the value.
563 * @param ch The <code>char</code> value to be printed
565 public synchronized void println (char ch
)
568 print(work
, 0, 1, true);
572 * This method prints an array of characters to the stream. The actual
573 * value printed depends on the system default encoding.
575 * This method prints a line termination sequence after printing the value.
577 * @param charArray The array of characters to print.
579 public void println (char[] charArray
)
581 print(charArray
, 0, charArray
.length
, true);
585 * This method writes a byte of data to the stream. If auto-flush is
586 * enabled, printing a newline character will cause the stream to be
587 * flushed after the character is written.
589 * @param oneByte The byte to be written
591 public void write (int oneByte
)
595 out
.write (oneByte
& 0xff);
597 if (auto_flush
&& (oneByte
== '\n'))
600 catch (InterruptedIOException iioe
)
602 Thread
.currentThread ().interrupt ();
604 catch (IOException e
)
611 * This method writes <code>len</code> bytes from the specified array
612 * starting at index <code>offset</code> into the array.
614 * @param buffer The array of bytes to write
615 * @param offset The index into the array to start writing from
616 * @param len The number of bytes to write
618 public void write (byte[] buffer
, int offset
, int len
)
622 out
.write (buffer
, offset
, len
);
627 catch (InterruptedIOException iioe
)
629 Thread
.currentThread ().interrupt ();
631 catch (IOException e
)
638 public PrintStream
append(char c
)
645 public PrintStream
append(CharSequence cs
)
647 print(cs
== null ?
"null" : cs
.toString());
652 public PrintStream
append(CharSequence cs
, int start
, int end
)
654 print(cs
== null ?
"null" : cs
.subSequence(start
, end
).toString());
659 public PrintStream
printf(String format
, Object
... args
)
661 return format(format
, args
);
665 public PrintStream
printf(Locale locale
, String format
, Object
... args
)
667 return format(locale
, format
, args
);
671 public PrintStream
format(String format
, Object
... args
)
673 return format(Locale
.getDefault(), format
, args
);
677 public PrintStream
format(Locale locale
, String format
, Object
... args
)
679 Formatter f
= new Formatter(this, locale
);
680 f
.format(format
, args
);
683 } // class PrintStream