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. */
41 /*import java.util.Locale;
42 import java.util.Formatter;
44 import gnu.classpath.SystemProperties;
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 text. None
53 * of the methods in this class throw an exception. However, errors can be
54 * detected by calling the <code>checkError()</code> method. Additionally, this
55 * stream can be designated as "autoflush" when created so that any writes are
56 * automatically flushed to the underlying output sink when the current line is
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)
63 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
65 public class PrintStream extends FilterOutputStream // implements Appendable
68 * Notice the implementation is quite similar to OutputStreamWriter. This
69 * leads to some minor duplication, because neither inherits from the other,
70 * and we want to maximize performance.
73 // Line separator string.
74 private static final char[] line_separator = { '\n' }/*
84 private final String encoding;
87 * This boolean indicates whether or not an error has ever occurred on this
90 private boolean error_occurred = false;
93 * This is <code>true</code> if auto-flush is enabled, <code>false</code>
96 private final boolean auto_flush;
99 * This method initializes a new <code>PrintStream</code> object to write to
100 * the specified output File. Doesn't autoflush.
103 * The <code>File</code> to write to.
104 * @throws FileNotFoundException
105 * if an error occurs while opening the file.
109 public PrintStream(File file)
110 // throws FileNotFoundException
112 this(new FileOutputStream(file), false);
116 * This method initializes a new <code>PrintStream</code> object to write to
117 * the specified output File. Doesn't autoflush.
120 * The <code>File</code> to write to.
122 * The name of the character encoding to use for this object.
123 * @throws FileNotFoundException
124 * If an error occurs while opening the file.
125 * @throws UnsupportedEncodingException
126 * If the charset specified by <code>encoding</code> is invalid.
130 public PrintStream(File file, String encoding)
131 // throws FileNotFoundException,UnsupportedEncodingException
133 this(new FileOutputStream(file), false, encoding);
137 * This method initializes a new <code>PrintStream</code> object to write to
138 * the specified output File. Doesn't autoflush.
141 * The name of the <code>File</code> to write to.
142 * @throws FileNotFoundException
143 * if an error occurs while opening the file,
147 public PrintStream(String fileName)
148 // throws FileNotFoundException
150 this(new FileOutputStream(new File(fileName)), false);
154 * This method initializes a new <code>PrintStream</code> object to write to
155 * the specified output File. Doesn't autoflush.
158 * The name of the <code>File</code> to write to.
160 * The name of the character encoding to use for this object.
161 * @throws FileNotFoundException
162 * if an error occurs while opening the file.
163 * @throws UnsupportedEncodingException
164 * If the charset specified by <code>encoding</code> is invalid.
168 public PrintStream(String fileName, String encoding)
169 // throws FileNotFoundException,UnsupportedEncodingException
171 this(new FileOutputStream(new File(fileName)), false, encoding);
175 * This method initializes a new <code>PrintStream</code> object to write to
176 * the specified output sink. Doesn't autoflush.
179 * The <code>OutputStream</code> to write to.
181 public PrintStream(OutputStream out) {
186 * This method initializes a new <code>PrintStream</code> object to write to
187 * the specified output sink. This constructor also allows "auto-flush"
188 * functionality to be specified where the stream will be flushed after every
189 * <code>print</code> or <code>println</code> call, when the
190 * <code>write</code> methods with array arguments are called, or when a
191 * single new-line character is written.
195 * The <code>OutputStream</code> to write to.
197 * <code>true</code> to flush the stream after every line,
198 * <code>false</code> otherwise
200 public PrintStream(OutputStream out, boolean auto_flush) {
203 * String encoding; try { encoding =
204 * SystemProperties.getProperty("file.encoding"); } catch (SecurityException
205 * e){ encoding = "ISO8859_1"; } catch (IllegalArgumentException e){
206 * encoding = "ISO8859_1"; } catch (NullPointerException e){ encoding =
209 this.encoding = "ISO8859_1"; // encoding;
210 this.auto_flush = auto_flush;
214 * This method initializes a new <code>PrintStream</code> object to write to
215 * the specified output sink. This constructor also allows "auto-flush"
216 * functionality to be specified where the stream will be flushed after every
217 * <code>print</code> or <code>println</code> call, when the
218 * <code>write</code> methods with array arguments are called, or when a
219 * single new-line character is written.
223 * The <code>OutputStream</code> to write to.
225 * <code>true</code> to flush the stream after every line,
226 * <code>false</code> otherwise
228 * The name of the character encoding to use for this object.
230 public PrintStream(OutputStream out, boolean auto_flush, String encoding)
231 // throws UnsupportedEncodingException
235 new String(new byte[] { 0 }, encoding); // check if encoding is supported
236 this.encoding = encoding;
237 this.auto_flush = auto_flush;
241 * This method checks to see if an error has occurred on this stream. Note
242 * that once an error has occurred, this method will continue to report
243 * <code>true</code> forever for this stream. Before checking for an error
244 * condition, this method flushes the stream.
246 * @return <code>true</code> if an error has occurred, <code>false</code>
249 public boolean checkError() {
251 return error_occurred;
255 * This method can be called by subclasses to indicate that an error has
256 * occurred and should be reported by <code>checkError</code>.
258 protected void setError() {
259 error_occurred = true;
263 * This method closes this stream and all underlying streams.
265 public void close() {
267 * try { flush(); out.close(); } catch (InterruptedIOException iioe) {
268 * Thread.currentThread().interrupt(); } catch (IOException e) { setError
274 * This method flushes any buffered bytes to the underlying stream and then
275 * flushes that stream as well.
277 public void flush() {
279 * try { out.flush(); } catch (InterruptedIOException iioe) {
280 * Thread.currentThread().interrupt(); } catch (IOException e) { setError
285 private synchronized void print(String str, boolean println) {
287 * try { writeChars(str, 0, str.length()); if (println)
288 * writeChars(line_separator, 0, line_separator.length); if (auto_flush)
289 * flush(); } catch (InterruptedIOException iioe) {
290 * Thread.currentThread().interrupt(); } catch (IOException e) { setError
295 private synchronized void print(char[] chars, int pos, int len, boolean println) {
297 * try { writeChars(chars, pos, len); if (println)
298 * writeChars(line_separator, 0, line_separator.length); if (auto_flush)
299 * flush(); } catch (InterruptedIOException iioe) {
300 * Thread.currentThread().interrupt(); } catch (IOException e) { setError
305 private void writeChars(char[] buf, int offset, int count)
306 // throws IOException
309 * byte[] bytes = (new String(buf, offset, count)).getBytes(encoding);
310 * out.write(bytes, 0, bytes.length);
314 private void writeChars(String str, int offset, int count)
315 // throws IOException
318 * byte[] bytes = str.substring(offset, offset+count).getBytes(encoding);
319 * out.write(bytes, 0, bytes.length);
324 * This methods prints a boolean value to the stream. <code>true</code> values
325 * are printed as "true" and <code>false</code> values are printed as "false".
328 * The <code>boolean</code> value to print
330 public void print(boolean bool) {
331 print(String.valueOf(bool), false);
335 * This method prints an integer to the stream. The value printed is
336 * determined using the <code>String.valueOf()</code> method.
339 * The <code>int</code> value to be printed
341 public void print(int inum) {
342 print(String.valueOf(inum), false);
346 * This method prints a long to the stream. The value printed is determined
347 * using the <code>String.valueOf()</code> method.
350 * The <code>long</code> value to be printed
352 public void print(long lnum) {
353 print(String.valueOf(lnum), false);
357 * This method prints a float to the stream. The value printed is determined
358 * using the <code>String.valueOf()</code> method.
361 * The <code>float</code> value to be printed
363 public void print(float fnum) {
364 print(String.valueOf(fnum), false);
368 * This method prints a double to the stream. The value printed is determined
369 * using the <code>String.valueOf()</code> method.
372 * The <code>double</code> value to be printed
374 public void print(double dnum) {
375 print(String.valueOf(dnum), false);
379 * This method prints an <code>Object</code> to the stream. The actual value
380 * printed is determined by calling the <code>String.valueOf()</code> method.
383 * The <code>Object</code> to print.
385 public void print(Object obj) {
386 print(obj == null ? "null" : obj.toString(), false);
390 * This method prints a <code>String</code> to the stream. The actual value
391 * printed depends on the system default encoding.
394 * The <code>String</code> to print.
396 public void print(String str) {
397 print(str == null ? "null" : str, false);
401 * This method prints a char to the stream. The actual value printed is
402 * determined by the character encoding in use.
405 * The <code>char</code> value to be printed
407 public synchronized void print(char ch) {
408 print(new char[] { ch }, 0, 1, false);
412 * This method prints an array of characters to the stream. The actual value
413 * printed depends on the system default encoding.
416 * The array of characters to print.
418 public void print(char[] charArray) {
419 print(charArray, 0, charArray.length, false);
423 * This method prints a line separator sequence to the stream. The value
424 * printed is determined by the system property <xmp>line.separator</xmp> and
425 * is not necessarily the Unix '\n' newline character.
427 public void println() {
428 print(line_separator, 0, line_separator.length, false);
432 * This methods prints a boolean value to the stream. <code>true</code> values
433 * are printed as "true" and <code>false</code> values are printed as "false".
435 * This method prints a line termination sequence after printing the value.
438 * The <code>boolean</code> value to print
440 public void println(boolean bool) {
441 print(String.valueOf(bool), true);
445 * This method prints an integer to the stream. The value printed is
446 * determined using the <code>String.valueOf()</code> method.
448 * This method prints a line termination sequence after printing the value.
451 * The <code>int</code> value to be printed
453 public void println(int inum) {
454 print(String.valueOf(inum), true);
458 * This method prints a long to the stream. The value printed is determined
459 * using the <code>String.valueOf()</code> method.
461 * This method prints a line termination sequence after printing the value.
464 * The <code>long</code> value to be printed
466 public void println(long lnum) {
467 print(String.valueOf(lnum), true);
471 * This method prints a float to the stream. The value printed is determined
472 * using the <code>String.valueOf()</code> method.
474 * This method prints a line termination sequence after printing the value.
477 * The <code>float</code> value to be printed
479 public void println(float fnum) {
480 print(String.valueOf(fnum), true);
484 * This method prints a double to the stream. The value printed is determined
485 * using the <code>String.valueOf()</code> method.
487 * This method prints a line termination sequence after printing the value.
490 * The <code>double</code> value to be printed
492 public void println(double dnum) {
493 print(String.valueOf(dnum), true);
497 * This method prints an <code>Object</code> to the stream. The actual value
498 * printed is determined by calling the <code>String.valueOf()</code> method.
500 * This method prints a line termination sequence after printing the value.
503 * The <code>Object</code> to print.
505 public void println(Object obj) {
506 print(obj == null ? "null" : obj.toString(), true);
510 * This method prints a <code>String</code> to the stream. The actual value
511 * printed depends on the system default encoding.
513 * This method prints a line termination sequence after printing the value.
516 * The <code>String</code> to print.
518 public void println(String str) {
519 print(str == null ? "null" : str, true);
523 * This method prints a char to the stream. The actual value printed is
524 * determined by the character encoding in use.
526 * This method prints a line termination sequence after printing the value.
529 * The <code>char</code> value to be printed
531 public synchronized void println(char ch) {
532 print(new char[] { ch }, 0, 1, true);
536 * This method prints an array of characters to the stream. The actual value
537 * printed depends on the system default encoding.
539 * This method prints a line termination sequence after printing the value.
542 * The array of characters to print.
544 public void println(char[] charArray) {
545 print(charArray, 0, charArray.length, true);
549 * This method writes a byte of data to the stream. If auto-flush is enabled,
550 * printing a newline character will cause the stream to be flushed after the
551 * character is written.
554 * The byte to be written
556 public void write(int oneByte) {
558 * try { out.write (oneByte & 0xff);
560 * if (auto_flush && (oneByte == '\n')) flush (); } catch
561 * (InterruptedIOException iioe) { Thread.currentThread ().interrupt (); }
562 * catch (IOException e) { setError (); }
567 * This method writes <code>len</code> bytes from the specified array starting
568 * at index <code>offset</code> into the array.
571 * The array of bytes to write
573 * The index into the array to start writing from
575 * The number of bytes to write
577 public void write(byte[] buffer, int offset, int len) {
579 * try { out.write (buffer, offset, len);
581 * if (auto_flush) flush (); } catch (InterruptedIOException iioe) {
582 * Thread.currentThread ().interrupt (); } catch (IOException e) { setError
588 public PrintStream append(char c) {
595 * public PrintStream append(CharSequence cs) { print(cs == null ? "null" :
596 * cs.toString()); return this; }
601 * public PrintStream append(CharSequence cs, int start, int end) { print(cs
602 * == null ? "null" : cs.subSequence(start, end).toString()); return this; }
607 * public PrintStream printf(String format, Object... args) { return this;
608 * //format(format, args); }
613 * public PrintStream printf(Locale locale, String format, Object... args) {
614 * return format(locale, format, args); }
619 * public PrintStream format(String format, Object... args) { return this;
620 * //format(Locale.getDefault(), format, args); }
625 * public PrintStream format(Locale locale, String format, Object... args) {
626 * Formatter f = new Formatter(this, locale); f.format(format, args); return
629 } // class PrintStream