1 /* PrintStream.java -- OutputStream for printing output
2 Copyright (C) 1998, 1999, 2001, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
42 import java.util.Locale;
43 import java.util.Formatter;
45 import gnu.classpath.SystemProperties;
47 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
48 * "The Java Language Specification", ISBN 0-201-63451-1
49 * Status: Believed complete and correct to 1.3
53 * This class prints Java primitive values and object to a stream as
54 * text. None of the methods in this class throw an exception. However,
55 * errors can be detected by calling the <code>checkError()</code> method.
56 * Additionally, this stream can be designated as "autoflush" when
57 * created so that any writes are automatically flushed to the underlying
58 * output sink when the current line is terminated.
60 * This class converts char's into byte's using the system default encoding.
62 * @author Aaron M. Renn (arenn@urbanophile.com)
63 * @author Tom Tromey (tromey@cygnus.com)
64 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
66 public class PrintStream extends FilterOutputStream implements Appendable
68 /* Notice the implementation is quite similar to OutputStreamWriter.
69 * This leads to some minor duplication, because neither inherits
70 * from the other, and we want to maximize performance. */
72 // Line separator string.
73 private static final char[] line_separator
74 = SystemProperties.getProperty("line.separator", "\n").toCharArray();
79 private String encoding;
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 initializes a new <code>PrintStream</code> object to write
95 * to the specified output File. Doesn't autoflush.
97 * @param file The <code>File</code> to write to.
98 * @throws FileNotFoundException if an error occurs while opening the file.
102 public PrintStream (File file)
103 throws FileNotFoundException
105 this (new FileOutputStream(file), false);
109 * This method initializes a new <code>PrintStream</code> object to write
110 * to the specified output File. Doesn't autoflush.
112 * @param file The <code>File</code> to write to.
113 * @param encoding The name of the character encoding to use for this
115 * @throws FileNotFoundException If an error occurs while opening the file.
116 * @throws UnsupportedEncodingException If the charset specified by
117 * <code>encoding</code> is invalid.
121 public PrintStream (File file, String encoding)
122 throws FileNotFoundException,UnsupportedEncodingException
124 this (new FileOutputStream(file), false, encoding);
128 * This method initializes a new <code>PrintStream</code> object to write
129 * to the specified output File. Doesn't autoflush.
131 * @param fileName The name of the <code>File</code> to write to.
132 * @throws FileNotFoundException if an error occurs while opening the file,
136 public PrintStream (String fileName)
137 throws FileNotFoundException
139 this (new FileOutputStream(new File(fileName)), false);
143 * This method initializes a new <code>PrintStream</code> object to write
144 * to the specified output File. Doesn't autoflush.
146 * @param fileName The name of the <code>File</code> to write to.
147 * @param encoding The name of the character encoding to use for this
149 * @throws FileNotFoundException if an error occurs while opening the file.
150 * @throws UnsupportedEncodingException If the charset specified by
151 * <code>encoding</code> is invalid.
155 public PrintStream (String fileName, String encoding)
156 throws FileNotFoundException,UnsupportedEncodingException
158 this (new FileOutputStream(new File(fileName)), false, encoding);
162 * This method initializes a new <code>PrintStream</code> object to write
163 * to the specified output sink. Doesn't autoflush.
165 * @param out The <code>OutputStream</code> to write to.
167 public PrintStream (OutputStream out)
173 * This method initializes a new <code>PrintStream</code> object to write
174 * to the specified output sink. This constructor also allows "auto-flush"
175 * functionality to be specified where the stream will be flushed after
176 * every <code>print</code> or <code>println</code> call, when the
177 * <code>write</code> methods with array arguments are called, or when a
178 * single new-line character is written.
181 * @param out The <code>OutputStream</code> to write to.
182 * @param auto_flush <code>true</code> to flush the stream after every
183 * line, <code>false</code> otherwise
185 public PrintStream (OutputStream out, boolean auto_flush)
190 this.encoding = SystemProperties.getProperty("file.encoding");
191 } catch (SecurityException e){
192 this.encoding = "ISO8859_1";
193 } catch (IllegalArgumentException e){
194 this.encoding = "ISO8859_1";
195 } catch (NullPointerException e){
196 this.encoding = "ISO8859_1";
198 this.auto_flush = auto_flush;
202 * This method initializes a new <code>PrintStream</code> object to write
203 * to the specified output sink. This constructor also allows "auto-flush"
204 * functionality to be specified where the stream will be flushed after
205 * every <code>print</code> or <code>println</code> call, when the
206 * <code>write</code> methods with array arguments are called, or when a
207 * single new-line character is written.
210 * @param out The <code>OutputStream</code> to write to.
211 * @param auto_flush <code>true</code> to flush the stream after every
212 * line, <code>false</code> otherwise
213 * @param encoding The name of the character encoding to use for this
216 public PrintStream (OutputStream out, boolean auto_flush, String encoding)
217 throws UnsupportedEncodingException
221 new String(new byte[]{0}, encoding); // check if encoding is supported
222 this.encoding = encoding;
223 this.auto_flush = auto_flush;
227 * This method checks to see if an error has occurred on this stream. Note
228 * that once an error has occurred, this method will continue to report
229 * <code>true</code> forever for this stream. Before checking for an
230 * error condition, this method flushes the stream.
232 * @return <code>true</code> if an error has occurred,
233 * <code>false</code> otherwise
235 public boolean checkError ()
238 return error_occurred;
242 * This method can be called by subclasses to indicate that an error
243 * has occurred and should be reported by <code>checkError</code>.
245 protected void setError ()
247 error_occurred = true;
251 * This method closes this stream and all underlying streams.
260 catch (InterruptedIOException iioe)
262 Thread.currentThread().interrupt();
264 catch (IOException e)
271 * This method flushes any buffered bytes to the underlying stream and
272 * then flushes that stream as well.
280 catch (InterruptedIOException iioe)
282 Thread.currentThread().interrupt();
284 catch (IOException e)
290 private synchronized void print (String str, boolean println)
294 writeChars(str, 0, str.length());
296 writeChars(line_separator, 0, line_separator.length);
300 catch (InterruptedIOException iioe)
302 Thread.currentThread().interrupt();
304 catch (IOException e)
310 private synchronized void print (char[] chars, int pos, int len,
315 writeChars(chars, pos, len);
317 writeChars(line_separator, 0, line_separator.length);
321 catch (InterruptedIOException iioe)
323 Thread.currentThread().interrupt();
325 catch (IOException e)
331 private void writeChars(char[] buf, int offset, int count)
334 byte[] bytes = (new String(buf, offset, count)).getBytes(encoding);
335 out.write(bytes, 0, bytes.length);
338 private void writeChars(String str, int offset, int count)
341 byte[] bytes = str.substring(offset, offset+count).getBytes(encoding);
342 out.write(bytes, 0, bytes.length);
346 * This methods prints a boolean value to the stream. <code>true</code>
347 * values are printed as "true" and <code>false</code> values are printed
350 * @param bool The <code>boolean</code> value to print
352 public void print (boolean bool)
354 print(String.valueOf(bool), false);
358 * This method prints an integer to the stream. The value printed is
359 * determined using the <code>String.valueOf()</code> method.
361 * @param inum The <code>int</code> value to be printed
363 public void print (int inum)
365 print(String.valueOf(inum), false);
369 * This method prints a long to the stream. The value printed is
370 * determined using the <code>String.valueOf()</code> method.
372 * @param lnum The <code>long</code> value to be printed
374 public void print (long lnum)
376 print(String.valueOf(lnum), false);
380 * This method prints a float to the stream. The value printed is
381 * determined using the <code>String.valueOf()</code> method.
383 * @param fnum The <code>float</code> value to be printed
385 public void print (float fnum)
387 print(String.valueOf(fnum), false);
391 * This method prints a double to the stream. The value printed is
392 * determined using the <code>String.valueOf()</code> method.
394 * @param dnum The <code>double</code> value to be printed
396 public void print (double dnum)
398 print(String.valueOf(dnum), false);
402 * This method prints an <code>Object</code> to the stream. The actual
403 * value printed is determined by calling the <code>String.valueOf()</code>
406 * @param obj The <code>Object</code> to print.
408 public void print (Object obj)
410 print(obj == null ? "null" : obj.toString(), false);
414 * This method prints a <code>String</code> to the stream. The actual
415 * value printed depends on the system default encoding.
417 * @param str The <code>String</code> to print.
419 public void print (String str)
421 print(str == null ? "null" : str, false);
425 * This method prints a char to the stream. The actual value printed is
426 * determined by the character encoding in use.
428 * @param ch The <code>char</code> value to be printed
430 public synchronized void print (char ch)
432 print(new char[]{ch}, 0, 1, false);
436 * This method prints an array of characters to the stream. The actual
437 * value printed depends on the system default encoding.
439 * @param charArray The array of characters to print.
441 public void print (char[] charArray)
443 print(charArray, 0, charArray.length, false);
447 * This method prints a line separator sequence to the stream. The value
448 * printed is determined by the system property <xmp>line.separator</xmp>
449 * and is not necessarily the Unix '\n' newline character.
451 public void println ()
453 print(line_separator, 0, line_separator.length, false);
457 * This methods prints a boolean value to the stream. <code>true</code>
458 * values are printed as "true" and <code>false</code> values are printed
461 * This method prints a line termination sequence after printing the value.
463 * @param bool The <code>boolean</code> value to print
465 public void println (boolean bool)
467 print(String.valueOf(bool), true);
471 * This method prints an integer to the stream. The value printed is
472 * determined using the <code>String.valueOf()</code> method.
474 * This method prints a line termination sequence after printing the value.
476 * @param inum The <code>int</code> value to be printed
478 public void println (int inum)
480 print(String.valueOf(inum), true);
484 * This method prints a long to the stream. The value printed is
485 * determined using the <code>String.valueOf()</code> method.
487 * This method prints a line termination sequence after printing the value.
489 * @param lnum The <code>long</code> value to be printed
491 public void println (long lnum)
493 print(String.valueOf(lnum), true);
497 * This method prints a float to the stream. The value printed is
498 * determined using the <code>String.valueOf()</code> method.
500 * This method prints a line termination sequence after printing the value.
502 * @param fnum The <code>float</code> value to be printed
504 public void println (float fnum)
506 print(String.valueOf(fnum), true);
510 * This method prints a double to the stream. The value printed is
511 * determined using the <code>String.valueOf()</code> method.
513 * This method prints a line termination sequence after printing the value.
515 * @param dnum The <code>double</code> value to be printed
517 public void println (double dnum)
519 print(String.valueOf(dnum), true);
523 * This method prints an <code>Object</code> to the stream. The actual
524 * value printed is determined by calling the <code>String.valueOf()</code>
527 * This method prints a line termination sequence after printing the value.
529 * @param obj The <code>Object</code> to print.
531 public void println (Object obj)
533 print(obj == null ? "null" : obj.toString(), true);
537 * This method prints a <code>String</code> to the stream. The actual
538 * value printed depends on the system default encoding.
540 * This method prints a line termination sequence after printing the value.
542 * @param str The <code>String</code> to print.
544 public void println (String str)
546 print (str == null ? "null" : str, true);
550 * This method prints a char to the stream. The actual value printed is
551 * determined by the character encoding in use.
553 * This method prints a line termination sequence after printing the value.
555 * @param ch The <code>char</code> value to be printed
557 public synchronized void println (char ch)
559 print(new char[]{ch}, 0, 1, true);
563 * This method prints an array of characters to the stream. The actual
564 * value printed depends on the system default encoding.
566 * This method prints a line termination sequence after printing the value.
568 * @param charArray The array of characters to print.
570 public void println (char[] charArray)
572 print(charArray, 0, charArray.length, true);
576 * This method writes a byte of data to the stream. If auto-flush is
577 * enabled, printing a newline character will cause the stream to be
578 * flushed after the character is written.
580 * @param oneByte The byte to be written
582 public void write (int oneByte)
586 out.write (oneByte & 0xff);
588 if (auto_flush && (oneByte == '\n'))
591 catch (InterruptedIOException iioe)
593 Thread.currentThread ().interrupt ();
595 catch (IOException e)
602 * This method writes <code>len</code> bytes from the specified array
603 * starting at index <code>offset</code> into the array.
605 * @param buffer The array of bytes to write
606 * @param offset The index into the array to start writing from
607 * @param len The number of bytes to write
609 public void write (byte[] buffer, int offset, int len)
613 out.write (buffer, offset, len);
618 catch (InterruptedIOException iioe)
620 Thread.currentThread ().interrupt ();
622 catch (IOException e)
629 public PrintStream append(char c)
636 public PrintStream append(CharSequence cs)
638 print(cs == null ? "null" : cs.toString());
643 public PrintStream append(CharSequence cs, int start, int end)
645 print(cs == null ? "null" : cs.subSequence(start, end).toString());
650 public PrintStream printf(String format, Object... args)
652 return format(format, args);
656 public PrintStream printf(Locale locale, String format, Object... args)
658 return format(locale, format, args);
662 public PrintStream format(String format, Object... args)
664 return format(Locale.getDefault(), format, args);
668 public PrintStream format(Locale locale, String format, Object... args)
670 Formatter f = new Formatter(this, locale);
671 f.format(format, args);
674 } // class PrintStream