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 PrintStreamS extends PrintStream //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()*/;
77 * This method initializes a new <code>PrintStream</code> object to write
78 * to the specified output File. Doesn't autoflush.
80 * @param file The <code>File</code> to write to.
81 * @throws FileNotFoundException if an error occurs while opening the file.
85 public PrintStreamS (File file)
86 //throws FileNotFoundException
92 * This method initializes a new <code>PrintStream</code> object to write
93 * to the specified output File. Doesn't autoflush.
95 * @param file The <code>File</code> to write to.
96 * @param encoding The name of the character encoding to use for this
98 * @throws FileNotFoundException If an error occurs while opening the file.
99 * @throws UnsupportedEncodingException If the charset specified by
100 * <code>encoding</code> is invalid.
104 public PrintStreamS (File file, String encoding)
105 //throws FileNotFoundException,UnsupportedEncodingException
107 super (file, encoding);
111 * This method initializes a new <code>PrintStream</code> object to write
112 * to the specified output File. Doesn't autoflush.
114 * @param fileName The name of the <code>File</code> to write to.
115 * @throws FileNotFoundException if an error occurs while opening the file,
119 public PrintStreamS (String fileName)
120 //throws FileNotFoundException
126 * This method initializes a new <code>PrintStream</code> object to write
127 * to the specified output File. Doesn't autoflush.
129 * @param fileName The name of the <code>File</code> to write to.
130 * @param encoding The name of the character encoding to use for this
132 * @throws FileNotFoundException if an error occurs while opening the file.
133 * @throws UnsupportedEncodingException If the charset specified by
134 * <code>encoding</code> is invalid.
138 public PrintStreamS (String fileName, String encoding)
139 //throws FileNotFoundException,UnsupportedEncodingException
145 * This method initializes a new <code>PrintStream</code> object to write
146 * to the specified output sink. Doesn't autoflush.
148 * @param out The <code>OutputStream</code> to write to.
150 public PrintStreamS (OutputStream out)
156 * This method initializes a new <code>PrintStream</code> object to write
157 * to the specified output sink. This constructor also allows "auto-flush"
158 * functionality to be specified where the stream will be flushed after
159 * every <code>print</code> or <code>println</code> call, when the
160 * <code>write</code> methods with array arguments are called, or when a
161 * single new-line character is written.
164 * @param out The <code>OutputStream</code> to write to.
165 * @param auto_flush <code>true</code> to flush the stream after every
166 * line, <code>false</code> otherwise
168 public PrintStreamS (OutputStream out, boolean auto_flush)
170 super (out, auto_flush);
174 * This method initializes a new <code>PrintStream</code> object to write
175 * to the specified output sink. This constructor also allows "auto-flush"
176 * functionality to be specified where the stream will be flushed after
177 * every <code>print</code> or <code>println</code> call, when the
178 * <code>write</code> methods with array arguments are called, or when a
179 * single new-line character is written.
182 * @param out The <code>OutputStream</code> to write to.
183 * @param auto_flush <code>true</code> to flush the stream after every
184 * line, <code>false</code> otherwise
185 * @param encoding The name of the character encoding to use for this
188 public PrintStreamS (OutputStream out, boolean auto_flush, String encoding)
189 //throws UnsupportedEncodingException
191 super (out, auto_flush, encoding);
195 * This method checks to see if an error has occurred on this stream. Note
196 * that once an error has occurred, this method will continue to report
197 * <code>true</code> forever for this stream. Before checking for an
198 * error condition, this method flushes the stream.
200 * @return <code>true</code> if an error has occurred,
201 * <code>false</code> otherwise
203 public boolean checkError ()
206 return error_occurred;
210 * This method can be called by subclasses to indicate that an error
211 * has occurred and should be reported by <code>checkError</code>.
213 protected void setError ()
215 error_occurred = true;
219 * This method closes this stream and all underlying streams.
228 catch (InterruptedIOException iioe)
230 Thread.currentThread().interrupt();
232 catch (IOException e)
239 * This method flushes any buffered bytes to the underlying stream and
240 * then flushes that stream as well.
248 catch (InterruptedIOException iioe)
250 Thread.currentThread().interrupt();
252 catch (IOException e)
258 private synchronized void print (String str, boolean println)
262 writeChars(str, 0, str.length());
264 writeChars(line_separator, 0, line_separator.length);
268 catch (InterruptedIOException iioe)
270 Thread.currentThread().interrupt();
272 catch (IOException e)
277 System.out.println("=== " + str);
279 System.out.print("=== " + str);
283 private synchronized void print (char[] chars, int pos, int len,
288 writeChars(chars, pos, len);
290 writeChars(line_separator, 0, line_separator.length);
294 catch (InterruptedIOException iioe)
296 Thread.currentThread().interrupt();
298 catch (IOException e)
302 String str = new String(chars, pos, len);
304 System.out.println("=== " + str);
306 System.out.print("=== " + str);
310 private void writeChars(char[] buf, int offset, int count)
313 byte[] bytes = (new String(buf, offset, count)).getBytes();
314 write(bytes, 0, bytes.length);
317 private void writeChars(String str, int offset, int count)
320 byte[] bytes = str.substring(offset, offset+count).getBytes();
321 write(bytes, 0, bytes.length);
325 * This methods prints a boolean value to the stream. <code>true</code>
326 * values are printed as "true" and <code>false</code> values are printed
329 * @param bool The <code>boolean</code> value to print
331 public void print (boolean bool)
333 System.out.print("=== " + String.valueOf(bool));
337 * This method prints an integer to the stream. The value printed is
338 * determined using the <code>String.valueOf()</code> method.
340 * @param inum The <code>int</code> value to be printed
342 public void print (int inum)
344 System.out.print("=== " + inum);
348 * This method prints a long to the stream. The value printed is
349 * determined using the <code>String.valueOf()</code> method.
351 * @param lnum The <code>long</code> value to be printed
353 public void print (long lnum)
355 System.out.print("=== " + lnum);
359 * This method prints a float to the stream. The value printed is
360 * determined using the <code>String.valueOf()</code> method.
362 * @param fnum The <code>float</code> value to be printed
364 public void print (float fnum)
366 System.out.print("=== " + fnum);
370 * This method prints a double to the stream. The value printed is
371 * determined using the <code>String.valueOf()</code> method.
373 * @param dnum The <code>double</code> value to be printed
375 public void print (double dnum)
377 System.out.print("=== " + dnum);
381 * This method prints an <code>Object</code> to the stream. The actual
382 * value printed is determined by calling the <code>String.valueOf()</code>
385 * @param obj The <code>Object</code> to print.
387 public void print (Object obj)
389 System.out.print("=== " + (obj == null ? "null" : obj.toString()));
393 * This method prints a <code>String</code> to the stream. The actual
394 * value printed depends on the system default encoding.
396 * @param str The <code>String</code> to print.
398 public void print (String str)
400 System.out.print("=== " + (str == null ? "null" : str));
404 * This method prints a char to the stream. The actual value printed is
405 * determined by the character encoding in use.
407 * @param ch The <code>char</code> value to be printed
409 public synchronized void print (char ch)
411 String str = new String(ch);
412 System.out.print("=== " + str);
416 * This method prints an array of characters to the stream. The actual
417 * value printed depends on the system default encoding.
419 * @param charArray The array of characters to print.
421 public void print (char[] charArray)
423 String str = new String(charArray, 0, charArray.length);
424 System.out.print("=== " + str);
428 * This method prints a line separator sequence to the stream. The value
429 * printed is determined by the system property <xmp>line.separator</xmp>
430 * and is not necessarily the Unix '\n' newline character.
432 public void println ()
434 System.out.println("=== ");
438 * This methods prints a boolean value to the stream. <code>true</code>
439 * values are printed as "true" and <code>false</code> values are printed
442 * This method prints a line termination sequence after printing the value.
444 * @param bool The <code>boolean</code> value to print
446 public void println (boolean bool)
448 System.out.println("=== " + String.valueOf(bool));
452 * This method prints an integer to the stream. The value printed is
453 * determined using the <code>String.valueOf()</code> method.
455 * This method prints a line termination sequence after printing the value.
457 * @param inum The <code>int</code> value to be printed
459 public void println (int inum)
461 System.out.println("=== " + inum);
465 * This method prints a long to the stream. The value printed is
466 * determined using the <code>String.valueOf()</code> method.
468 * This method prints a line termination sequence after printing the value.
470 * @param lnum The <code>long</code> value to be printed
472 public void println (long lnum)
474 System.out.println("=== " + lnum);
478 * This method prints a float to the stream. The value printed is
479 * determined using the <code>String.valueOf()</code> method.
481 * This method prints a line termination sequence after printing the value.
483 * @param fnum The <code>float</code> value to be printed
485 public void println (float fnum)
487 System.out.println("=== " + fnum);
491 * This method prints a double to the stream. The value printed is
492 * determined using the <code>String.valueOf()</code> method.
494 * This method prints a line termination sequence after printing the value.
496 * @param dnum The <code>double</code> value to be printed
498 public void println (double dnum)
500 System.out.println("=== " + dnum);
504 * This method prints an <code>Object</code> to the stream. The actual
505 * value printed is determined by calling the <code>String.valueOf()</code>
508 * This method prints a line termination sequence after printing the value.
510 * @param obj The <code>Object</code> to print.
512 public void println (Object obj)
514 System.out.println("=== " + (obj == null ? "null" : obj.toString()));
518 * This method prints a <code>String</code> to the stream. The actual
519 * value printed depends on the system default encoding.
521 * This method prints a line termination sequence after printing the value.
523 * @param str The <code>String</code> to print.
525 public void println (String str)
527 System.out.println ("=== " + (str == null ? "null" : str));
531 * This method prints a char to the stream. The actual value printed is
532 * determined by the character encoding in use.
534 * This method prints a line termination sequence after printing the value.
536 * @param ch The <code>char</code> value to be printed
538 public synchronized void println (char ch)
540 String str = new String(ch);
541 System.out.println("=== " + str);
545 * This method prints an array of characters 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 charArray The array of characters to print.
552 public void println (char[] charArray)
554 String str = new String(charArray, 0, charArray.length);
555 System.out.println("=== " + str);
559 * This method writes a byte of data to the stream. If auto-flush is
560 * enabled, printing a newline character will cause the stream to be
561 * flushed after the character is written.
563 * @param oneByte The byte to be written
565 public void write (int oneByte)
569 out.write (oneByte & 0xff);
571 if (auto_flush && (oneByte == '\n'))
574 catch (InterruptedIOException iioe)
576 Thread.currentThread ().interrupt ();
578 catch (IOException e)
582 System.out.print("=== " + oneByte);
586 * This method writes <code>len</code> bytes from the specified array
587 * starting at index <code>offset</code> into the array.
589 * @param buffer The array of bytes to write
590 * @param offset The index into the array to start writing from
591 * @param len The number of bytes to write
593 public void write (byte[] buffer, int offset, int len)
597 out.write (buffer, offset, len);
602 catch (InterruptedIOException iioe)
604 Thread.currentThread ().interrupt ();
606 catch (IOException e)
610 String str = new String(buffer, offset,len);
611 System.out.print("=== " + str);
615 public PrintStream append(char c)
617 System.out.print("=== " + c);
622 /*public PrintStream append(CharSequence cs)
624 print(cs == null ? "null" : cs.toString());
629 /*public PrintStream append(CharSequence cs, int start, int end)
631 print(cs == null ? "null" : cs.subSequence(start, end).toString());
636 /*public PrintStream printf(String format, Object... args)
638 return this; //format(format, args);
642 /*public PrintStream printf(Locale locale, String format, Object... args)
644 return format(locale, format, args);
648 /*public PrintStream format(String format, Object... args)
650 return this; //format(Locale.getDefault(), format, args);
654 /*public PrintStream format(Locale locale, String format, Object... args)
656 Formatter f = new Formatter(this, locale);
657 f.format(format, args);
660 } // class PrintStream