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 intializes a new <code>PrintStream</code> object to write
127 * to the specified output sink. This constructor also allows "auto-flush"
128 * functionality to be specified where the stream will be flushed after
129 * every <code>print</code> or <code>println</code> call, when the
130 * <code>write</code> methods with array arguments are called, or when a
131 * single new-line character is written.
134 * @param out The <code>OutputStream</code> to write to.
135 * @param auto_flush <code>true</code> to flush the stream after every
136 * line, <code>false</code> otherwise
137 * @param encoding The name of the character encoding to use for this
140 public PrintStream (OutputStream out
, boolean auto_flush
, String encoding
)
141 throws UnsupportedEncodingException
145 converter
= UnicodeToBytes
.getEncoder (encoding
);
146 this.auto_flush
= auto_flush
;
150 * This method checks to see if an error has occurred on this stream. Note
151 * that once an error has occurred, this method will continue to report
152 * <code>true</code> forever for this stream. Before checking for an
153 * error condition, this method flushes the stream.
155 * @return <code>true</code> if an error has occurred,
156 * <code>false</code> otherwise
158 public boolean checkError ()
161 return error_occurred
;
165 * This method can be called by subclasses to indicate that an error
166 * has occurred and should be reported by <code>checkError</code>.
168 protected void setError ()
170 error_occurred
= true;
174 * This method closes this stream and all underlying streams.
180 converter
.setFinished();
181 writeChars(new char[0], 0, 0);
185 catch (InterruptedIOException iioe
)
187 Thread
.currentThread().interrupt();
189 catch (IOException e
)
196 * This method flushes any buffered bytes to the underlying stream and
197 * then flushes that stream as well.
205 catch (InterruptedIOException iioe
)
207 Thread
.currentThread().interrupt();
209 catch (IOException e
)
215 private synchronized void print (String str
, boolean println
)
219 writeChars(str
, 0, str
.length());
221 writeChars(line_separator
, 0, line_separator
.length
);
225 catch (InterruptedIOException iioe
)
227 Thread
.currentThread().interrupt();
229 catch (IOException e
)
235 private synchronized void print (char[] chars
, int pos
, int len
,
240 writeChars(chars
, pos
, len
);
242 writeChars(line_separator
, 0, line_separator
.length
);
246 catch (InterruptedIOException iioe
)
248 Thread
.currentThread().interrupt();
250 catch (IOException e
)
256 private void writeChars(char[] buf
, int offset
, int count
)
261 converter
.setOutput(work_bytes
, 0);
262 int converted
= converter
.write(buf
, offset
, count
);
265 out
.write(work_bytes
, 0, converter
.count
);
267 while (count
> 0 || converter
.havePendingBytes());
270 private void writeChars(String str
, int offset
, int count
)
275 converter
.setOutput(work_bytes
, 0);
276 int converted
= converter
.write(str
, offset
, count
, work
);
279 out
.write(work_bytes
, 0, converter
.count
);
281 while (count
> 0 || converter
.havePendingBytes());
285 * This methods prints a boolean value to the stream. <code>true</code>
286 * values are printed as "true" and <code>false</code> values are printed
289 * @param bool The <code>boolean</code> value to print
291 public void print (boolean bool
)
293 print(String
.valueOf(bool
), false);
297 * This method prints an integer to the stream. The value printed is
298 * determined using the <code>String.valueOf()</code> method.
300 * @param inum The <code>int</code> value to be printed
302 public void print (int inum
)
304 print(String
.valueOf(inum
), false);
308 * This method prints a long to the stream. The value printed is
309 * determined using the <code>String.valueOf()</code> method.
311 * @param lnum The <code>long</code> value to be printed
313 public void print (long lnum
)
315 print(String
.valueOf(lnum
), false);
319 * This method prints a float to the stream. The value printed is
320 * determined using the <code>String.valueOf()</code> method.
322 * @param fnum The <code>float</code> value to be printed
324 public void print (float fnum
)
326 print(String
.valueOf(fnum
), false);
330 * This method prints a double to the stream. The value printed is
331 * determined using the <code>String.valueOf()</code> method.
333 * @param dnum The <code>double</code> value to be printed
335 public void print (double dnum
)
337 print(String
.valueOf(dnum
), false);
341 * This method prints an <code>Object</code> to the stream. The actual
342 * value printed is determined by calling the <code>String.valueOf()</code>
345 * @param obj The <code>Object</code> to print.
347 public void print (Object obj
)
349 print(obj
== null ?
"null" : obj
.toString(), false);
353 * This method prints a <code>String</code> to the stream. The actual
354 * value printed depends on the system default encoding.
356 * @param str The <code>String</code> to print.
358 public void print (String str
)
360 print(str
== null ?
"null" : str
, false);
364 * This method prints a char to the stream. The actual value printed is
365 * determined by the character encoding in use.
367 * @param ch The <code>char</code> value to be printed
369 public synchronized void print (char ch
)
372 print(work
, 0, 1, false);
376 * This method prints an array of characters to the stream. The actual
377 * value printed depends on the system default encoding.
379 * @param charArray The array of characters to print.
381 public void print (char[] charArray
)
383 print(charArray
, 0, charArray
.length
, false);
387 * This method prints a line separator sequence to the stream. The value
388 * printed is determined by the system property <xmp>line.separator</xmp>
389 * and is not necessarily the Unix '\n' newline character.
391 public void println ()
393 print(line_separator
, 0, line_separator
.length
, false);
397 * This methods prints a boolean value to the stream. <code>true</code>
398 * values are printed as "true" and <code>false</code> values are printed
401 * This method prints a line termination sequence after printing the value.
403 * @param bool The <code>boolean</code> value to print
405 public void println (boolean bool
)
407 print(String
.valueOf(bool
), true);
411 * This method prints an integer to the stream. The value printed is
412 * determined using the <code>String.valueOf()</code> method.
414 * This method prints a line termination sequence after printing the value.
416 * @param inum The <code>int</code> value to be printed
418 public void println (int inum
)
420 print(String
.valueOf(inum
), true);
424 * This method prints a long to the stream. The value printed is
425 * determined using the <code>String.valueOf()</code> method.
427 * This method prints a line termination sequence after printing the value.
429 * @param lnum The <code>long</code> value to be printed
431 public void println (long lnum
)
433 print(String
.valueOf(lnum
), true);
437 * This method prints a float to the stream. The value printed is
438 * determined using the <code>String.valueOf()</code> method.
440 * This method prints a line termination sequence after printing the value.
442 * @param fnum The <code>float</code> value to be printed
444 public void println (float fnum
)
446 print(String
.valueOf(fnum
), true);
450 * This method prints a double to the stream. The value printed is
451 * determined using the <code>String.valueOf()</code> method.
453 * This method prints a line termination sequence after printing the value.
455 * @param dnum The <code>double</code> value to be printed
457 public void println (double dnum
)
459 print(String
.valueOf(dnum
), true);
463 * This method prints an <code>Object</code> to the stream. The actual
464 * value printed is determined by calling the <code>String.valueOf()</code>
467 * This method prints a line termination sequence after printing the value.
469 * @param obj The <code>Object</code> to print.
471 public void println (Object obj
)
473 print(obj
== null ?
"null" : obj
.toString(), true);
477 * This method prints a <code>String</code> to the stream. The actual
478 * value printed depends on the system default encoding.
480 * This method prints a line termination sequence after printing the value.
482 * @param str The <code>String</code> to print.
484 public void println (String str
)
486 print (str
== null ?
"null" : str
, true);
490 * This method prints a char to the stream. The actual value printed is
491 * determined by the character encoding in use.
493 * This method prints a line termination sequence after printing the value.
495 * @param ch The <code>char</code> value to be printed
497 public synchronized void println (char ch
)
500 print(work
, 0, 1, true);
504 * This method prints an array of characters to the stream. The actual
505 * value printed depends on the system default encoding.
507 * This method prints a line termination sequence after printing the value.
509 * @param charArray The array of characters to print.
511 public void println (char[] charArray
)
513 print(charArray
, 0, charArray
.length
, true);
517 * This method writes a byte of data to the stream. If auto-flush is
518 * enabled, printing a newline character will cause the stream to be
519 * flushed after the character is written.
521 * @param oneByte The byte to be written
523 public void write (int oneByte
)
527 out
.write (oneByte
& 0xff);
529 if (auto_flush
&& (oneByte
== '\n'))
532 catch (InterruptedIOException iioe
)
534 Thread
.currentThread ().interrupt ();
536 catch (IOException e
)
543 * This method writes <code>len</code> bytes from the specified array
544 * starting at index <code>offset</code> into the array.
546 * @param buffer The array of bytes to write
547 * @param offset The index into the array to start writing from
548 * @param len The number of bytes to write
550 public void write (byte[] buffer
, int offset
, int len
)
554 out
.write (buffer
, offset
, len
);
559 catch (InterruptedIOException iioe
)
561 Thread
.currentThread ().interrupt ();
563 catch (IOException e
)
570 public PrintStream
append(char c
)
577 public PrintStream
append(CharSequence cs
)
579 print(cs
== null ?
"null" : cs
.toString());
584 public PrintStream
append(CharSequence cs
, int start
, int end
)
586 print(cs
== null ?
"null" : cs
.subSequence(start
, end
).toString());
591 public PrintStream
printf(String format
, Object
... args
)
593 return format(format
, args
);
597 public PrintStream
printf(Locale locale
, String format
, Object
... args
)
599 return format(locale
, format
, args
);
603 public PrintStream
format(String format
, Object
... args
)
605 return format(Locale
.getDefault(), format
, args
);
609 public PrintStream
format(Locale locale
, String format
, Object
... args
)
611 Formatter f
= new Formatter(this, locale
);
612 f
.format(format
, args
);
615 } // class PrintStream