Merge from the pain train
[official-gcc.git] / libjava / java / io / PrintWriter.java
blobe03f2f85a876d248de294c08a4221141b46fc12d
1 /* PrintWriter.java -- prints primitive values and objects to a stream as text
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
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)
9 any later version.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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
24 combination.
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. */
38 package java.io;
40 /* Written using "Java Class Libraries", 2nd edition, plus online
41 * API docs for JDK 1.2 beta from http://www.javasoft.com.
42 * Status: Believed complete and correct.
43 * However, should use native methods for conversion.
46 /**
47 * This class prints Java primitive values and objects to a stream as
48 * text. None of the methods in this class throw an exception. However,
49 * errors can be detected by calling the <code>checkError()</code> method.
50 * Additionally, this stream can be designated as "autoflush" when
51 * created so that any writes are automatically flushed to the underlying
52 * output sink whenever one of the <code>println</code> methods is
53 * called. (Note that this differs from the <code>PrintStream</code>
54 * class which also auto-flushes when it encounters a newline character
55 * in the chars written).
57 * @author Per Bothner (bothner@cygnus.com)
58 * @author Aaron M. Renn (arenn@urbanophile.com)
59 * @date April 17, 1998.
61 public class PrintWriter extends Writer
63 /**
64 * <code>true</code> if auto-flush is enabled, <code>false</code> otherwise
66 private boolean autoflush;
68 /**
69 * This boolean indicates whether or not an error has ever occurred
70 * on this stream.
72 private boolean error;
74 /**
75 * This is the underlying <code>Writer</code> we are sending output
76 * to
78 protected Writer out;
80 /**
81 * This method intializes a new <code>PrintWriter</code> object to write
82 * to the specified output sink. The form of the constructor does not
83 * enable auto-flush functionality.
85 * @param wr The <code>Writer</code> to write to.
87 public PrintWriter(Writer wr)
89 super(wr.lock);
90 this.out = wr;
93 /**
94 * This method intializes a new <code>PrintWriter</code> object to write
95 * to the specified output sink. This constructor also allows "auto-flush"
96 * functionality to be specified where the stream will be flushed after
97 * every line is terminated or newline character is written.
99 * @param wr The <code>Writer</code> to write to.
100 * @param autoflush <code>true</code> to flush the stream after every
101 * line, <code>false</code> otherwise
103 public PrintWriter(Writer wr, boolean autoflush)
105 super(wr.lock);
106 this.out = wr;
107 this.autoflush = autoflush;
111 * This method initializes a new <code>PrintWriter</code> object to write
112 * to the specified <code>OutputStream</code>. Characters will be converted
113 * to chars using the system default encoding. Auto-flush functionality
114 * will not be enabled.
116 * @param out The <code>OutputStream</code> to write to
118 public PrintWriter(OutputStream out)
120 super();
121 this.out = new OutputStreamWriter(out);
122 this.lock = this.out;
126 * This method initializes a new <code>PrintWriter</code> object to write
127 * to the specified <code>OutputStream</code>. Characters will be converted
128 * to chars using the system default encoding. This form of the
129 * constructor allows auto-flush functionality to be enabled if desired
131 * @param out The <code>OutputStream</code> to write to
132 * @param autoflush <code>true</code> to flush the stream after every
133 * <code>println</code> call, <code>false</code> otherwise.
135 public PrintWriter(OutputStream out, boolean autoflush)
137 this(out);
138 this.autoflush = autoflush;
142 * This method can be called by subclasses to indicate that an error
143 * has occurred and should be reported by <code>checkError</code>.
145 protected void setError()
147 error = true;
151 * This method checks to see if an error has occurred on this stream. Note
152 * that once an error has occurred, this method will continue to report
153 * <code>true</code> forever for this stream. Before checking for an
154 * error condition, this method flushes the stream.
156 * @return <code>true</code> if an error has occurred,
157 * <code>false</code> otherwise
159 public boolean checkError()
161 flush();
162 return error;
166 * This method flushes any buffered chars to the underlying stream and
167 * then flushes that stream as well.
169 public void flush()
173 out.flush();
175 catch (IOException ex)
177 error = true;
182 * This method closes this stream and all underlying streams.
184 public void close()
188 out.close();
190 catch (IOException ex)
192 error = true;
197 * This method prints a <code>String</code> to the stream. The actual
198 * value printed depends on the system default encoding.
200 * @param str The <code>String</code> to print.
202 public void print(String str)
204 write(str == null ? "null" : str);
208 * This method prints a char to the stream. The actual value printed is
209 * determined by the character encoding in use.
211 * @param ch The <code>char</code> value to be printed
213 public void print(char ch)
215 write((int) ch);
219 * This method prints an array of characters to the stream. The actual
220 * value printed depends on the system default encoding.
222 * @param charArray The array of characters to print.
224 public void print(char[] charArray)
226 write(charArray, 0, charArray.length);
230 * This methods prints a boolean value to the stream. <code>true</code>
231 * values are printed as "true" and <code>false</code> values are printed
232 * as "false".
234 * @param bool The <code>boolean</code> value to print
236 public void print(boolean bool)
238 // We purposely call write() and not print() here. This preserves
239 // compatibility with JDK 1.2.
240 write (bool ? "true" : "false");
244 * This method prints an integer to the stream. The value printed is
245 * determined using the <code>String.valueOf()</code> method.
247 * @param inum The <code>int</code> value to be printed
249 public void print(int inum)
251 // We purposely call write() and not print() here. This preserves
252 // compatibility with JDK 1.2.
253 write(Integer.toString(inum));
257 * This method prints a long to the stream. The value printed is
258 * determined using the <code>String.valueOf()</code> method.
260 * @param lnum The <code>long</code> value to be printed
262 public void print(long lnum)
264 // We purposely call write() and not print() here. This preserves
265 // compatibility with JDK 1.2.
266 write(Long.toString(lnum));
270 * This method prints a float to the stream. The value printed is
271 * determined using the <code>String.valueOf()</code> method.
273 * @param fnum The <code>float</code> value to be printed
275 public void print(float fnum)
277 // We purposely call write() and not print() here. This preserves
278 // compatibility with JDK 1.2.
279 write(Float.toString(fnum));
283 * This method prints a double to the stream. The value printed is
284 * determined using the <code>String.valueOf()</code> method.
286 * @param dnum The <code>double</code> value to be printed
288 public void print(double dnum)
290 // We purposely call write() and not print() here. This preserves
291 // compatibility with JDK 1.2.
292 write(Double.toString(dnum));
296 * This method prints an <code>Object</code> to the stream. The actual
297 * value printed is determined by calling the <code>String.valueOf()</code>
298 * method.
300 * @param obj The <code>Object</code> to print.
302 public void print(Object obj)
304 // We purposely call write() and not print() here. This preserves
305 // compatibility with JDK 1.2.
306 write(obj == null ? "null" : obj.toString());
310 * This is the system dependent line separator
312 private static final char[] line_separator
313 = System.getProperty("line.separator").toCharArray();
316 * This method prints a line separator sequence to the stream. The value
317 * printed is determined by the system property <xmp>line.separator</xmp>
318 * and is not necessarily the Unix '\n' newline character.
320 public void println()
322 synchronized (lock)
326 write(line_separator, 0, line_separator.length);
327 if (autoflush)
328 out.flush();
330 catch (IOException ex)
332 error = true;
338 * This methods prints a boolean value to the stream. <code>true</code>
339 * values are printed as "true" and <code>false</code> values are printed
340 * as "false".
342 * This method prints a line termination sequence after printing the value.
344 * @param bool The <code>boolean</code> value to print
346 public void println(boolean bool)
348 synchronized (lock)
350 print(bool);
351 println();
356 * This method prints an integer to the stream. The value printed is
357 * determined using the <code>String.valueOf()</code> method.
359 * This method prints a line termination sequence after printing the value.
361 * @param inum The <code>int</code> value to be printed
363 public void println(int inum)
365 synchronized (lock)
367 print(inum);
368 println();
373 * This method prints a long to the stream. The value printed is
374 * determined using the <code>String.valueOf()</code> method.
376 * This method prints a line termination sequence after printing the value.
378 * @param lnum The <code>long</code> value to be printed
380 public void println(long lnum)
382 synchronized (lock)
384 print(lnum);
385 println();
390 * This method prints a float to the stream. The value printed is
391 * determined using the <code>String.valueOf()</code> method.
393 * This method prints a line termination sequence after printing the value.
395 * @param fnum The <code>float</code> value to be printed
397 public void println(float fnum)
399 synchronized (lock)
401 print(fnum);
402 println();
407 * This method prints a double to the stream. The value printed is
408 * determined using the <code>String.valueOf()</code> method.
410 * This method prints a line termination sequence after printing the value.
412 * @param dnum The <code>double</code> value to be printed
414 public void println(double dnum)
416 synchronized (lock)
418 print(dnum);
419 println();
424 * This method prints an <code>Object</code> to the stream. The actual
425 * value printed is determined by calling the <code>String.valueOf()</code>
426 * method.
428 * This method prints a line termination sequence after printing the value.
430 * @param obj The <code>Object</code> to print.
432 public void println(Object obj)
434 synchronized (lock)
436 print(obj);
437 println();
442 * This method prints a <code>String</code> to the stream. The actual
443 * value printed depends on the system default encoding.
445 * This method prints a line termination sequence after printing the value.
447 * @param str The <code>String</code> to print.
449 public void println(String str)
451 synchronized (lock)
453 print(str);
454 println();
459 * This method prints a char to the stream. The actual value printed is
460 * determined by the character encoding in use.
462 * This method prints a line termination sequence after printing the value.
464 * @param ch The <code>char</code> value to be printed
466 public void println(char ch)
468 synchronized (lock)
470 print(ch);
471 println();
476 * This method prints an array of characters to the stream. The actual
477 * value printed depends on the system default encoding.
479 * This method prints a line termination sequence after printing the value.
481 * @param charArray The array of characters to print.
483 public void println(char[] charArray)
485 synchronized (lock)
487 print(charArray);
488 println();
493 * This method writes a single char to the stream.
495 * @param ch The char to be written, passed as a int
497 public void write(int ch)
501 out.write(ch);
503 catch (IOException ex)
505 error = true;
510 * This method writes <code>count</code> chars from the specified array
511 * starting at index <code>offset</code> into the array.
513 * @param charArray The array of chars to write
514 * @param offset The index into the array to start writing from
515 * @param count The number of chars to write
517 public void write(char[] charArray, int offset, int count)
521 out.write(charArray, offset, count);
523 catch (IOException ex)
525 error = true;
530 * This method writes <code>count</code> chars from the specified
531 * <code>String</code> to the output starting at character position
532 * <code>offset</code> into the <code>String</code>
534 * @param str The <code>String</code> to write chars from
535 * @param offset The offset into the <code>String</code> to start writing from
536 * @param count The number of chars to write.
538 public void write(String str, int offset, int count)
542 out.write(str, offset, count);
544 catch (IOException ex)
546 error = true;
551 * This method write all the chars in the specified array to the output.
553 * @param charArray The array of characters to write
555 public void write(char[] charArray)
557 write(charArray, 0, charArray.length);
561 * This method writes the contents of the specified <code>String</code>
562 * to the underlying stream.
564 * @param str The <code>String</code> to write
566 public void write(String str)
568 write(str, 0, str.length());