]> git.ipfire.org Git - thirdparty/gcc.git/blame - libjava/java/io/PrintWriter.java
BufferedWriter.java (BufferedWriter): Use existing lock of chained Writer when callin...
[thirdparty/gcc.git] / libjava / java / io / PrintWriter.java
CommitLineData
f74fd2bb
MW
1/* PrintWriter.java -- prints primitive values and objects to a stream as text
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
ee9dd372 3
f74fd2bb 4This file is part of GNU Classpath.
ee9dd372 5
f74fd2bb
MW
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
ee9dd372 10
f74fd2bb
MW
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
92aaa246
MW
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
f74fd2bb 37
ee9dd372
TT
38package java.io;
39
ee9dd372
TT
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.
44 */
45
c93aa804
MK
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).
56 *
57 * @author Per Bothner <bothner@cygnus.com>
58 * @author Aaron M. Renn <arenn@urbanophile.com>
59 * @date April 17, 1998.
60 */
ee9dd372
TT
61public class PrintWriter extends Writer
62{
26dfad44
TT
63 /**
64 * <code>true</code> if auto-flush is enabled, <code>false</code> otherwise
65 */
ee9dd372 66 private boolean autoflush;
26dfad44
TT
67
68 /**
18e1f2bd 69 * This boolean indicates whether or not an error has ever occurred
26dfad44
TT
70 * on this stream.
71 */
ee9dd372 72 private boolean error;
ee9dd372 73
26dfad44
TT
74 /**
75 * This is the underlying <code>Writer</code> we are sending output
76 * to
77 */
78 protected Writer out;
79
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.
84 *
85 * @param wr The <code>Writer</code> to write to.
86 */
ee9dd372
TT
87 public PrintWriter(Writer wr)
88 {
8246c778 89 super(wr.lock);
ee9dd372
TT
90 this.out = wr;
91 }
92
26dfad44
TT
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.
98 *
99 * @param wr The <code>Writer</code> to write to.
f4f5d1d6
MK
100 * @param autoflush <code>true</code> to flush the stream after every
101 * line, <code>false</code> otherwise
26dfad44 102 */
ee9dd372
TT
103 public PrintWriter(Writer wr, boolean autoflush)
104 {
8246c778 105 super(wr.lock);
ee9dd372
TT
106 this.out = wr;
107 this.autoflush = autoflush;
108 }
109
26dfad44
TT
110 /**
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.
115 *
116 * @param out The <code>OutputStream</code> to write to
117 */
ee9dd372
TT
118 public PrintWriter(OutputStream out)
119 {
120 super();
121 this.out = new OutputStreamWriter(out);
122 this.lock = this.out;
123 }
124
26dfad44
TT
125 /**
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
130 *
131 * @param out The <code>OutputStream</code> to write to
f4f5d1d6
MK
132 * @param autoflush <code>true</code> to flush the stream after every
133 * <code>println</code> call, <code>false</code> otherwise.
26dfad44 134 */
ee9dd372
TT
135 public PrintWriter(OutputStream out, boolean autoflush)
136 {
137 this(out);
138 this.autoflush = autoflush;
139 }
ee9dd372 140
26dfad44
TT
141 /**
142 * This method can be called by subclasses to indicate that an error
143 * has occurred and should be reported by <code>checkError</code>.
144 */
145 protected void setError()
146 {
147 error = true;
148 }
149
150 /**
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.
155 *
f4f5d1d6
MK
156 * @return <code>true</code> if an error has occurred,
157 * <code>false</code> otherwise
26dfad44 158 */
ee9dd372
TT
159 public boolean checkError()
160 {
161 flush();
162 return error;
163 }
164
26dfad44
TT
165 /**
166 * This method flushes any buffered chars to the underlying stream and
167 * then flushes that stream as well.
168 */
ee9dd372
TT
169 public void flush()
170 {
171 try
172 {
173 out.flush();
174 }
175 catch (IOException ex)
176 {
177 error = true;
178 }
179 }
180
26dfad44
TT
181 /**
182 * This method closes this stream and all underlying streams.
183 */
ee9dd372
TT
184 public void close()
185 {
186 try
187 {
188 out.close();
189 }
190 catch (IOException ex)
191 {
192 error = true;
193 }
194 }
195
26dfad44
TT
196 /**
197 * This method prints a <code>String</code> to the stream. The actual
198 * value printed depends on the system default encoding.
199 *
200 * @param str The <code>String</code> to print.
201 */
ee9dd372
TT
202 public void print(String str)
203 {
fb034e94 204 write(str == null ? "null" : str);
ee9dd372
TT
205 }
206
26dfad44
TT
207 /**
208 * This method prints a char to the stream. The actual value printed is
209 * determined by the character encoding in use.
210 *
211 * @param ch The <code>char</code> value to be printed
212 */
ee9dd372
TT
213 public void print(char ch)
214 {
215 write((int) ch);
216 }
217
26dfad44
TT
218 /**
219 * This method prints an array of characters to the stream. The actual
220 * value printed depends on the system default encoding.
221 *
222 * @param charArray The array of characters to print.
223 */
ee9dd372
TT
224 public void print(char[] charArray)
225 {
226 write(charArray, 0, charArray.length);
227 }
228
26dfad44
TT
229 /**
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".
233 *
234 * @param bool The <code>boolean</code> value to print
235 */
ee9dd372
TT
236 public void print(boolean bool)
237 {
fa948ac3
TT
238 // We purposely call write() and not print() here. This preserves
239 // compatibility with JDK 1.2.
240 write (bool ? "true" : "false");
ee9dd372
TT
241 }
242
26dfad44
TT
243 /**
244 * This method prints an integer to the stream. The value printed is
245 * determined using the <code>String.valueOf()</code> method.
246 *
247 * @param inum The <code>int</code> value to be printed
248 */
ee9dd372
TT
249 public void print(int inum)
250 {
fa948ac3
TT
251 // We purposely call write() and not print() here. This preserves
252 // compatibility with JDK 1.2.
253 write(Integer.toString(inum));
ee9dd372
TT
254 }
255
26dfad44
TT
256 /**
257 * This method prints a long to the stream. The value printed is
258 * determined using the <code>String.valueOf()</code> method.
259 *
260 * @param lnum The <code>long</code> value to be printed
261 */
ee9dd372
TT
262 public void print(long lnum)
263 {
fa948ac3
TT
264 // We purposely call write() and not print() here. This preserves
265 // compatibility with JDK 1.2.
266 write(Long.toString(lnum));
ee9dd372
TT
267 }
268
26dfad44
TT
269 /**
270 * This method prints a float to the stream. The value printed is
271 * determined using the <code>String.valueOf()</code> method.
272 *
273 * @param fnum The <code>float</code> value to be printed
274 */
ee9dd372
TT
275 public void print(float fnum)
276 {
fa948ac3
TT
277 // We purposely call write() and not print() here. This preserves
278 // compatibility with JDK 1.2.
279 write(Float.toString(fnum));
ee9dd372
TT
280 }
281
26dfad44
TT
282 /**
283 * This method prints a double to the stream. The value printed is
284 * determined using the <code>String.valueOf()</code> method.
285 *
286 * @param dnum The <code>double</code> value to be printed
287 */
ee9dd372
TT
288 public void print(double dnum)
289 {
fa948ac3
TT
290 // We purposely call write() and not print() here. This preserves
291 // compatibility with JDK 1.2.
292 write(Double.toString(dnum));
ee9dd372
TT
293 }
294
26dfad44
TT
295 /**
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.
299 *
300 * @param obj The <code>Object</code> to print.
301 */
ee9dd372
TT
302 public void print(Object obj)
303 {
fa948ac3
TT
304 // We purposely call write() and not print() here. This preserves
305 // compatibility with JDK 1.2.
306 write(obj == null ? "null" : obj.toString());
ee9dd372
TT
307 }
308
26dfad44
TT
309 /**
310 * This is the system dependent line separator
311 */
ee9dd372 312 private static final char[] line_separator
f4f5d1d6 313 = System.getProperty("line.separator").toCharArray();
ee9dd372 314
26dfad44
TT
315 /**
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.
319 */
ee9dd372
TT
320 public void println()
321 {
322 synchronized (lock)
323 {
26dfad44
TT
324 try
325 {
326 write(line_separator, 0, line_separator.length);
327 if (autoflush)
328 out.flush();
329 }
330 catch (IOException ex)
331 {
332 error = true;
333 }
ee9dd372
TT
334 }
335 }
336
26dfad44
TT
337 /**
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".
341 *
342 * This method prints a line termination sequence after printing the value.
343 *
344 * @param bool The <code>boolean</code> value to print
345 */
ee9dd372
TT
346 public void println(boolean bool)
347 {
348 synchronized (lock)
349 {
350 print(bool);
26dfad44 351 println();
ee9dd372
TT
352 }
353 }
26dfad44
TT
354
355 /**
356 * This method prints an integer to the stream. The value printed is
357 * determined using the <code>String.valueOf()</code> method.
358 *
359 * This method prints a line termination sequence after printing the value.
360 *
361 * @param inum The <code>int</code> value to be printed
362 */
ee9dd372
TT
363 public void println(int inum)
364 {
365 synchronized (lock)
366 {
367 print(inum);
26dfad44 368 println();
ee9dd372
TT
369 }
370 }
371
26dfad44
TT
372 /**
373 * This method prints a long to the stream. The value printed is
374 * determined using the <code>String.valueOf()</code> method.
375 *
376 * This method prints a line termination sequence after printing the value.
377 *
378 * @param lnum The <code>long</code> value to be printed
379 */
ee9dd372
TT
380 public void println(long lnum)
381 {
382 synchronized (lock)
383 {
384 print(lnum);
26dfad44 385 println();
ee9dd372
TT
386 }
387 }
388
26dfad44
TT
389 /**
390 * This method prints a float to the stream. The value printed is
391 * determined using the <code>String.valueOf()</code> method.
392 *
393 * This method prints a line termination sequence after printing the value.
394 *
395 * @param fnum The <code>float</code> value to be printed
396 */
ee9dd372
TT
397 public void println(float fnum)
398 {
399 synchronized (lock)
400 {
401 print(fnum);
26dfad44 402 println();
ee9dd372
TT
403 }
404 }
405
26dfad44
TT
406 /**
407 * This method prints a double to the stream. The value printed is
408 * determined using the <code>String.valueOf()</code> method.
409 *
410 * This method prints a line termination sequence after printing the value.
411 *
412 * @param dnum The <code>double</code> value to be printed
413 */
ee9dd372
TT
414 public void println(double dnum)
415 {
416 synchronized (lock)
417 {
418 print(dnum);
26dfad44 419 println();
ee9dd372
TT
420 }
421 }
422
26dfad44
TT
423 /**
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.
427 *
428 * This method prints a line termination sequence after printing the value.
429 *
430 * @param obj The <code>Object</code> to print.
431 */
ee9dd372
TT
432 public void println(Object obj)
433 {
434 synchronized (lock)
435 {
436 print(obj);
26dfad44 437 println();
ee9dd372
TT
438 }
439 }
440
26dfad44
TT
441 /**
442 * This method prints a <code>String</code> to the stream. The actual
443 * value printed depends on the system default encoding.
444 *
445 * This method prints a line termination sequence after printing the value.
446 *
447 * @param str The <code>String</code> to print.
448 */
ee9dd372
TT
449 public void println(String str)
450 {
451 synchronized (lock)
452 {
453 print(str);
26dfad44 454 println();
ee9dd372
TT
455 }
456 }
457
26dfad44
TT
458 /**
459 * This method prints a char to the stream. The actual value printed is
460 * determined by the character encoding in use.
461 *
462 * This method prints a line termination sequence after printing the value.
463 *
464 * @param ch The <code>char</code> value to be printed
465 */
ee9dd372
TT
466 public void println(char ch)
467 {
468 synchronized (lock)
469 {
470 print(ch);
26dfad44 471 println();
ee9dd372
TT
472 }
473 }
474
26dfad44
TT
475 /**
476 * This method prints an array of characters to the stream. The actual
477 * value printed depends on the system default encoding.
478 *
479 * This method prints a line termination sequence after printing the value.
480 *
481 * @param charArray The array of characters to print.
482 */
ee9dd372
TT
483 public void println(char[] charArray)
484 {
485 synchronized (lock)
486 {
487 print(charArray);
26dfad44 488 println();
ee9dd372
TT
489 }
490 }
491
26dfad44
TT
492 /**
493 * This method writes a single char to the stream.
494 *
495 * @param ch The char to be written, passed as a int
496 */
ee9dd372
TT
497 public void write(int ch)
498 {
499 try
500 {
501 out.write(ch);
502 }
503 catch (IOException ex)
504 {
505 error = true;
506 }
507 }
508
26dfad44
TT
509 /**
510 * This method writes <code>count</code> chars from the specified array
511 * starting at index <code>offset</code> into the array.
512 *
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
516 */
ee9dd372
TT
517 public void write(char[] charArray, int offset, int count)
518 {
519 try
520 {
521 out.write(charArray, offset, count);
522 }
523 catch (IOException ex)
524 {
525 error = true;
526 }
527 }
528
26dfad44
TT
529 /**
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>
533 *
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.
537 */
ee9dd372
TT
538 public void write(String str, int offset, int count)
539 {
540 try
541 {
542 out.write(str, offset, count);
543 }
544 catch (IOException ex)
545 {
546 error = true;
547 }
548 }
549
26dfad44
TT
550 /**
551 * This method write all the chars in the specified array to the output.
552 *
553 * @param charArray The array of characters to write
554 */
ee9dd372
TT
555 public void write(char[] charArray)
556 {
557 write(charArray, 0, charArray.length);
558 }
559
26dfad44
TT
560 /**
561 * This method writes the contents of the specified <code>String</code>
562 * to the underlying stream.
563 *
564 * @param str The <code>String</code> to write
565 */
ee9dd372
TT
566 public void write(String str)
567 {
568 write(str, 0, str.length());
569 }
570}
f4f5d1d6 571