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 = {'\n'}/*SystemProperties.getProperty("line.separator", "\n").toCharArray()*/;
79 private final 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 final 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 encoding = SystemProperties.getProperty("file.encoding");
191 } catch (SecurityException e){
192 encoding = "ISO8859_1";
193 } catch (IllegalArgumentException e){
194 encoding = "ISO8859_1";
195 } catch (NullPointerException e){
196 encoding = "ISO8859_1";
198 this.encoding = "ISO8859_1"; //encoding;
199 this.auto_flush = auto_flush;
203 * This method initializes a new <code>PrintStream</code> object to write
204 * to the specified output sink. This constructor also allows "auto-flush"
205 * functionality to be specified where the stream will be flushed after
206 * every <code>print</code> or <code>println</code> call, when the
207 * <code>write</code> methods with array arguments are called, or when a
208 * single new-line character is written.
211 * @param out The <code>OutputStream</code> to write to.
212 * @param auto_flush <code>true</code> to flush the stream after every
213 * line, <code>false</code> otherwise
214 * @param encoding The name of the character encoding to use for this
217 public PrintStream (OutputStream out, boolean auto_flush, String encoding)
218 //throws UnsupportedEncodingException
222 new String(new byte[]{0}, encoding); // check if encoding is supported
223 this.encoding = encoding;
224 this.auto_flush = auto_flush;
228 * This method checks to see if an error has occurred on this stream. Note
229 * that once an error has occurred, this method will continue to report
230 * <code>true</code> forever for this stream. Before checking for an
231 * error condition, this method flushes the stream.
233 * @return <code>true</code> if an error has occurred,
234 * <code>false</code> otherwise
236 public boolean checkError ()
239 return error_occurred;
243 * This method can be called by subclasses to indicate that an error
244 * has occurred and should be reported by <code>checkError</code>.
246 protected void setError ()
248 error_occurred = true;
252 * This method closes this stream and all underlying streams.
261 catch (InterruptedIOException iioe)
263 Thread.currentThread().interrupt();
265 catch (IOException e)
272 * This method flushes any buffered bytes to the underlying stream and
273 * then flushes that stream as well.
281 catch (InterruptedIOException iioe)
283 Thread.currentThread().interrupt();
285 catch (IOException e)
291 private synchronized void print (String str, boolean println)
295 writeChars(str, 0, str.length());
297 writeChars(line_separator, 0, line_separator.length);
301 catch (InterruptedIOException iioe)
303 Thread.currentThread().interrupt();
305 catch (IOException e)
311 private synchronized void print (char[] chars, int pos, int len,
316 writeChars(chars, pos, len);
318 writeChars(line_separator, 0, line_separator.length);
322 catch (InterruptedIOException iioe)
324 Thread.currentThread().interrupt();
326 catch (IOException e)
332 private void writeChars(char[] buf, int offset, int count)
335 /*byte[] bytes = (new String(buf, offset, count)).getBytes(encoding);
336 out.write(bytes, 0, bytes.length);*/
339 private void writeChars(String str, int offset, int count)
342 /*byte[] bytes = str.substring(offset, offset+count).getBytes(encoding);
343 out.write(bytes, 0, bytes.length);*/
347 * This methods prints a boolean value to the stream. <code>true</code>
348 * values are printed as "true" and <code>false</code> values are printed
351 * @param bool The <code>boolean</code> value to print
353 public void print (boolean bool)
355 print(String.valueOf(bool), false);
359 * This method prints an integer to the stream. The value printed is
360 * determined using the <code>String.valueOf()</code> method.
362 * @param inum The <code>int</code> value to be printed
364 public void print (int inum)
366 print(String.valueOf(inum), false);
370 * This method prints a long to the stream. The value printed is
371 * determined using the <code>String.valueOf()</code> method.
373 * @param lnum The <code>long</code> value to be printed
375 public void print (long lnum)
377 print(String.valueOf(lnum), false);
381 * This method prints a float to the stream. The value printed is
382 * determined using the <code>String.valueOf()</code> method.
384 * @param fnum The <code>float</code> value to be printed
386 public void print (float fnum)
388 print(String.valueOf(fnum), false);
392 * This method prints a double to the stream. The value printed is
393 * determined using the <code>String.valueOf()</code> method.
395 * @param dnum The <code>double</code> value to be printed
397 public void print (double dnum)
399 print(String.valueOf(dnum), false);
403 * This method prints an <code>Object</code> to the stream. The actual
404 * value printed is determined by calling the <code>String.valueOf()</code>
407 * @param obj The <code>Object</code> to print.
409 public void print (Object obj)
411 print(obj == null ? "null" : obj.toString(), false);
415 * This method prints a <code>String</code> to the stream. The actual
416 * value printed depends on the system default encoding.
418 * @param str The <code>String</code> to print.
420 public void print (String str)
422 print(str == null ? "null" : str, false);
426 * This method prints a char to the stream. The actual value printed is
427 * determined by the character encoding in use.
429 * @param ch The <code>char</code> value to be printed
431 public synchronized void print (char ch)
433 print(new char[]{ch}, 0, 1, false);
437 * This method prints an array of characters to the stream. The actual
438 * value printed depends on the system default encoding.
440 * @param charArray The array of characters to print.
442 public void print (char[] charArray)
444 print(charArray, 0, charArray.length, false);
448 * This method prints a line separator sequence to the stream. The value
449 * printed is determined by the system property <xmp>line.separator</xmp>
450 * and is not necessarily the Unix '\n' newline character.
452 public void println ()
454 print(line_separator, 0, line_separator.length, false);
458 * This methods prints a boolean value to the stream. <code>true</code>
459 * values are printed as "true" and <code>false</code> values are printed
462 * This method prints a line termination sequence after printing the value.
464 * @param bool The <code>boolean</code> value to print
466 public void println (boolean bool)
468 print(String.valueOf(bool), true);
472 * This method prints an integer to the stream. The value printed is
473 * determined using the <code>String.valueOf()</code> method.
475 * This method prints a line termination sequence after printing the value.
477 * @param inum The <code>int</code> value to be printed
479 public void println (int inum)
481 print(String.valueOf(inum), true);
485 * This method prints a long to the stream. The value printed is
486 * determined using the <code>String.valueOf()</code> method.
488 * This method prints a line termination sequence after printing the value.
490 * @param lnum The <code>long</code> value to be printed
492 public void println (long lnum)
494 print(String.valueOf(lnum), true);
498 * This method prints a float to the stream. The value printed is
499 * determined using the <code>String.valueOf()</code> method.
501 * This method prints a line termination sequence after printing the value.
503 * @param fnum The <code>float</code> value to be printed
505 public void println (float fnum)
507 print(String.valueOf(fnum), true);
511 * This method prints a double to the stream. The value printed is
512 * determined using the <code>String.valueOf()</code> method.
514 * This method prints a line termination sequence after printing the value.
516 * @param dnum The <code>double</code> value to be printed
518 public void println (double dnum)
520 print(String.valueOf(dnum), true);
524 * This method prints an <code>Object</code> to the stream. The actual
525 * value printed is determined by calling the <code>String.valueOf()</code>
528 * This method prints a line termination sequence after printing the value.
530 * @param obj The <code>Object</code> to print.
532 public void println (Object obj)
534 print(obj == null ? "null" : obj.toString(), true);
538 * This method prints a <code>String</code> to the stream. The actual
539 * value printed depends on the system default encoding.
541 * This method prints a line termination sequence after printing the value.
543 * @param str The <code>String</code> to print.
545 public void println (String str)
547 print (str == null ? "null" : str, true);
551 * This method prints a char to the stream. The actual value printed is
552 * determined by the character encoding in use.
554 * This method prints a line termination sequence after printing the value.
556 * @param ch The <code>char</code> value to be printed
558 public synchronized void println (char ch)
560 print(new char[]{ch}, 0, 1, true);
564 * This method prints an array of characters to the stream. The actual
565 * value printed depends on the system default encoding.
567 * This method prints a line termination sequence after printing the value.
569 * @param charArray The array of characters to print.
571 public void println (char[] charArray)
573 print(charArray, 0, charArray.length, true);
577 * This method writes a byte of data to the stream. If auto-flush is
578 * enabled, printing a newline character will cause the stream to be
579 * flushed after the character is written.
581 * @param oneByte The byte to be written
583 public void write (int oneByte)
587 out.write (oneByte & 0xff);
589 if (auto_flush && (oneByte == '\n'))
592 catch (InterruptedIOException iioe)
594 Thread.currentThread ().interrupt ();
596 catch (IOException e)
603 * This method writes <code>len</code> bytes from the specified array
604 * starting at index <code>offset</code> into the array.
606 * @param buffer The array of bytes to write
607 * @param offset The index into the array to start writing from
608 * @param len The number of bytes to write
610 public void write (byte[] buffer, int offset, int len)
614 out.write (buffer, offset, len);
619 catch (InterruptedIOException iioe)
621 Thread.currentThread ().interrupt ();
623 catch (IOException e)
630 public PrintStream append(char c)
636 public PrintStream append(String s)
638 print(s==null?"null":s);
643 /*public PrintStream append(CharSequence cs)
645 print(cs == null ? "null" : cs.toString());
650 /*public PrintStream append(CharSequence cs, int start, int end)
652 print(cs == null ? "null" : cs.subSequence(start, end).toString());
657 /*public PrintStream printf(String format, Object... args)
659 return this; //format(format, args);
663 /*public PrintStream printf(Locale locale, String format, Object... args)
665 return format(locale, format, args);
669 /*public PrintStream format(String format, Object... args)
671 return this; //format(Locale.getDefault(), format, args);
675 /*public PrintStream format(Locale locale, String format, Object... args)
677 Formatter f = new Formatter(this, locale);
678 f.format(format, args);
681 } // class PrintStream