Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/java/io/PrintWriter.java
+++ new/src/share/classes/java/io/PrintWriter.java
1 1 /*
2 2 * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25
26 26 package java.io;
27 27
28 +import java.util.Objects;
28 29 import java.util.Formatter;
29 30 import java.util.Locale;
31 +import java.nio.charset.Charset;
32 +import java.nio.charset.IllegalCharsetNameException;
30 33
31 34 /**
32 35 * Prints formatted representations of objects to a text-output stream. This
33 36 * class implements all of the <tt>print</tt> methods found in {@link
34 37 * PrintStream}. It does not contain methods for writing raw bytes, for which
35 38 * a program should use unencoded byte streams.
36 39 *
37 40 * <p> Unlike the {@link PrintStream} class, if automatic flushing is enabled
38 41 * it will be done only when one of the <tt>println</tt>, <tt>printf</tt>, or
39 42 * <tt>format</tt> methods is invoked, rather than whenever a newline character
40 43 * happens to be output. These methods use the platform's own notion of line
41 44 * separator rather than the newline character.
42 45 *
43 46 * <p> Methods in this class never throw I/O exceptions, although some of its
44 47 * constructors may. The client may inquire as to whether any errors have
45 48 * occurred by invoking {@link #checkError checkError()}.
46 49 *
47 50 * @author Frank Yellin
48 51 * @author Mark Reinhold
49 52 * @since JDK1.1
50 53 */
51 54
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
52 55 public class PrintWriter extends Writer {
53 56
54 57 /**
55 58 * The underlying character-output stream of this
56 59 * <code>PrintWriter</code>.
57 60 *
58 61 * @since 1.2
59 62 */
60 63 protected Writer out;
61 64
62 - private boolean autoFlush = false;
65 + private final boolean autoFlush;
63 66 private boolean trouble = false;
64 67 private Formatter formatter;
65 68 private PrintStream psOut = null;
66 69
67 70 /**
68 71 * Line separator string. This is the value of the line.separator
69 72 * property at the moment that the stream was created.
70 73 */
71 - private String lineSeparator;
74 + private final String lineSeparator;
72 75
73 76 /**
77 + * Returns a charset object for the given charset name.
78 + * @throws NullPointerException is csn is null
79 + * @throws UnsupportedEncodingException if the charset is not supported
80 + */
81 + private static Charset toCharset(String csn)
82 + throws UnsupportedEncodingException {
83 + Objects.nonNull(csn, "charsetName");
84 + try {
85 + return Charset.forName(csn);
86 + } catch (IllegalCharsetNameException unused) {
87 + /* swallow this exception since UnsupportedEncodingException
88 + * will be thrown */
89 + }
90 + throw new UnsupportedEncodingException(csn);
91 + }
92 +
93 + /**
74 94 * Creates a new PrintWriter, without automatic line flushing.
75 95 *
76 96 * @param out A character-output stream
77 97 */
78 98 public PrintWriter (Writer out) {
79 99 this(out, false);
80 100 }
81 101
82 102 /**
83 103 * Creates a new PrintWriter.
84 104 *
85 105 * @param out A character-output stream
86 106 * @param autoFlush A boolean; if true, the <tt>println</tt>,
87 107 * <tt>printf</tt>, or <tt>format</tt> methods will
88 108 * flush the output buffer
89 109 */
90 110 public PrintWriter(Writer out,
91 111 boolean autoFlush) {
92 112 super(out);
93 113 this.out = out;
94 114 this.autoFlush = autoFlush;
95 115 lineSeparator = java.security.AccessController.doPrivileged(
96 116 new sun.security.action.GetPropertyAction("line.separator"));
97 117 }
98 118
99 119 /**
100 120 * Creates a new PrintWriter, without automatic line flushing, from an
101 121 * existing OutputStream. This convenience constructor creates the
102 122 * necessary intermediate OutputStreamWriter, which will convert characters
103 123 * into bytes using the default character encoding.
104 124 *
105 125 * @param out An output stream
106 126 *
107 127 * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
108 128 */
109 129 public PrintWriter(OutputStream out) {
110 130 this(out, false);
111 131 }
112 132
113 133 /**
114 134 * Creates a new PrintWriter from an existing OutputStream. This
115 135 * convenience constructor creates the necessary intermediate
116 136 * OutputStreamWriter, which will convert characters into bytes using the
117 137 * default character encoding.
118 138 *
119 139 * @param out An output stream
120 140 * @param autoFlush A boolean; if true, the <tt>println</tt>,
121 141 * <tt>printf</tt>, or <tt>format</tt> methods will
122 142 * flush the output buffer
123 143 *
124 144 * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
125 145 */
126 146 public PrintWriter(OutputStream out, boolean autoFlush) {
127 147 this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
128 148
129 149 // save print stream for error propagation
130 150 if (out instanceof java.io.PrintStream) {
131 151 psOut = (PrintStream) out;
132 152 }
133 153 }
134 154
135 155 /**
136 156 * Creates a new PrintWriter, without automatic line flushing, with the
137 157 * specified file name. This convenience constructor creates the necessary
138 158 * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
139 159 * which will encode characters using the {@linkplain
140 160 * java.nio.charset.Charset#defaultCharset() default charset} for this
141 161 * instance of the Java virtual machine.
142 162 *
143 163 * @param fileName
144 164 * The name of the file to use as the destination of this writer.
145 165 * If the file exists then it will be truncated to zero size;
146 166 * otherwise, a new file will be created. The output will be
147 167 * written to the file and is buffered.
148 168 *
149 169 * @throws FileNotFoundException
150 170 * If the given string does not denote an existing, writable
151 171 * regular file and a new regular file of that name cannot be
152 172 * created, or if some other error occurs while opening or
153 173 * creating the file
154 174 *
155 175 * @throws SecurityException
156 176 * If a security manager is present and {@link
↓ open down ↓ |
73 lines elided |
↑ open up ↑ |
157 177 * SecurityManager#checkWrite checkWrite(fileName)} denies write
158 178 * access to the file
159 179 *
160 180 * @since 1.5
161 181 */
162 182 public PrintWriter(String fileName) throws FileNotFoundException {
163 183 this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
164 184 false);
165 185 }
166 186
187 + /* Private constructor*/
188 + private PrintWriter(Charset charset, File file) throws FileNotFoundException {
189 + this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)),
190 + false);
191 + }
192 +
167 193 /**
168 194 * Creates a new PrintWriter, without automatic line flushing, with the
169 195 * specified file name and charset. This convenience constructor creates
170 196 * the necessary intermediate {@link java.io.OutputStreamWriter
171 197 * OutputStreamWriter}, which will encode characters using the provided
172 198 * charset.
173 199 *
174 200 * @param fileName
175 201 * The name of the file to use as the destination of this writer.
176 202 * If the file exists then it will be truncated to zero size;
177 203 * otherwise, a new file will be created. The output will be
178 204 * written to the file and is buffered.
179 205 *
180 206 * @param csn
181 207 * The name of a supported {@linkplain java.nio.charset.Charset
182 208 * charset}
183 209 *
184 210 * @throws FileNotFoundException
185 211 * If the given string does not denote an existing, writable
186 212 * regular file and a new regular file of that name cannot be
187 213 * created, or if some other error occurs while opening or
188 214 * creating the file
189 215 *
190 216 * @throws SecurityException
191 217 * If a security manager is present and {@link
192 218 * SecurityManager#checkWrite checkWrite(fileName)} denies write
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
193 219 * access to the file
194 220 *
195 221 * @throws UnsupportedEncodingException
196 222 * If the named charset is not supported
197 223 *
198 224 * @since 1.5
199 225 */
200 226 public PrintWriter(String fileName, String csn)
201 227 throws FileNotFoundException, UnsupportedEncodingException
202 228 {
203 - this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), csn)),
204 - false);
229 + this(toCharset(csn), new File(fileName));
205 230 }
206 231
207 232 /**
208 233 * Creates a new PrintWriter, without automatic line flushing, with the
209 234 * specified file. This convenience constructor creates the necessary
210 235 * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
211 236 * which will encode characters using the {@linkplain
212 237 * java.nio.charset.Charset#defaultCharset() default charset} for this
213 238 * instance of the Java virtual machine.
214 239 *
215 240 * @param file
216 241 * The file to use as the destination of this writer. If the file
217 242 * exists then it will be truncated to zero size; otherwise, a new
218 243 * file will be created. The output will be written to the file
219 244 * and is buffered.
220 245 *
221 246 * @throws FileNotFoundException
222 247 * If the given file object does not denote an existing, writable
223 248 * regular file and a new regular file of that name cannot be
224 249 * created, or if some other error occurs while opening or
225 250 * creating the file
226 251 *
227 252 * @throws SecurityException
228 253 * If a security manager is present and {@link
229 254 * SecurityManager#checkWrite checkWrite(file.getPath())}
230 255 * denies write access to the file
231 256 *
232 257 * @since 1.5
233 258 */
234 259 public PrintWriter(File file) throws FileNotFoundException {
235 260 this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
236 261 false);
237 262 }
238 263
239 264 /**
240 265 * Creates a new PrintWriter, without automatic line flushing, with the
241 266 * specified file and charset. This convenience constructor creates the
242 267 * necessary intermediate {@link java.io.OutputStreamWriter
243 268 * OutputStreamWriter}, which will encode characters using the provided
244 269 * charset.
245 270 *
246 271 * @param file
247 272 * The file to use as the destination of this writer. If the file
248 273 * exists then it will be truncated to zero size; otherwise, a new
249 274 * file will be created. The output will be written to the file
250 275 * and is buffered.
251 276 *
252 277 * @param csn
253 278 * The name of a supported {@linkplain java.nio.charset.Charset
254 279 * charset}
255 280 *
256 281 * @throws FileNotFoundException
257 282 * If the given file object does not denote an existing, writable
258 283 * regular file and a new regular file of that name cannot be
259 284 * created, or if some other error occurs while opening or
260 285 * creating the file
261 286 *
262 287 * @throws SecurityException
263 288 * If a security manager is present and {@link
264 289 * SecurityManager#checkWrite checkWrite(file.getPath())}
↓ open down ↓ |
50 lines elided |
↑ open up ↑ |
265 290 * denies write access to the file
266 291 *
267 292 * @throws UnsupportedEncodingException
268 293 * If the named charset is not supported
269 294 *
270 295 * @since 1.5
271 296 */
272 297 public PrintWriter(File file, String csn)
273 298 throws FileNotFoundException, UnsupportedEncodingException
274 299 {
275 - this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), csn)),
276 - false);
300 + this(toCharset(csn), file);
277 301 }
278 302
279 303 /** Checks to make sure that the stream has not been closed */
280 304 private void ensureOpen() throws IOException {
281 305 if (out == null)
282 306 throw new IOException("Stream closed");
283 307 }
284 308
285 309 /**
286 310 * Flushes the stream.
287 311 * @see #checkError()
288 312 */
289 313 public void flush() {
290 314 try {
291 315 synchronized (lock) {
292 316 ensureOpen();
293 317 out.flush();
294 318 }
295 319 }
296 320 catch (IOException x) {
297 321 trouble = true;
298 322 }
299 323 }
300 324
301 325 /**
302 326 * Closes the stream and releases any system resources associated
303 327 * with it. Closing a previously closed stream has no effect.
304 328 *
305 329 * @see #checkError()
306 330 */
307 331 public void close() {
308 332 try {
309 333 synchronized (lock) {
310 334 if (out == null)
311 335 return;
312 336 out.close();
313 337 out = null;
314 338 }
315 339 }
316 340 catch (IOException x) {
317 341 trouble = true;
318 342 }
319 343 }
320 344
321 345 /**
322 346 * Flushes the stream if it's not closed and checks its error state.
323 347 *
324 348 * @return <code>true</code> if the print stream has encountered an error,
325 349 * either on the underlying output stream or during a format
326 350 * conversion.
327 351 */
328 352 public boolean checkError() {
329 353 if (out != null) {
330 354 flush();
331 355 }
332 356 if (out instanceof java.io.PrintWriter) {
333 357 PrintWriter pw = (PrintWriter) out;
334 358 return pw.checkError();
335 359 } else if (psOut != null) {
336 360 return psOut.checkError();
337 361 }
338 362 return trouble;
339 363 }
340 364
341 365 /**
342 366 * Indicates that an error has occurred.
343 367 *
344 368 * <p> This method will cause subsequent invocations of {@link
345 369 * #checkError()} to return <tt>true</tt> until {@link
346 370 * #clearError()} is invoked.
347 371 */
348 372 protected void setError() {
349 373 trouble = true;
350 374 }
351 375
352 376 /**
353 377 * Clears the error state of this stream.
354 378 *
355 379 * <p> This method will cause subsequent invocations of {@link
356 380 * #checkError()} to return <tt>false</tt> until another write
357 381 * operation fails and invokes {@link #setError()}.
358 382 *
359 383 * @since 1.6
360 384 */
361 385 protected void clearError() {
362 386 trouble = false;
363 387 }
364 388
365 389 /*
366 390 * Exception-catching, synchronized output operations,
367 391 * which also implement the write() methods of Writer
368 392 */
369 393
370 394 /**
371 395 * Writes a single character.
372 396 * @param c int specifying a character to be written.
373 397 */
374 398 public void write(int c) {
375 399 try {
376 400 synchronized (lock) {
377 401 ensureOpen();
378 402 out.write(c);
379 403 }
380 404 }
381 405 catch (InterruptedIOException x) {
382 406 Thread.currentThread().interrupt();
383 407 }
384 408 catch (IOException x) {
385 409 trouble = true;
386 410 }
387 411 }
388 412
389 413 /**
390 414 * Writes A Portion of an array of characters.
391 415 * @param buf Array of characters
392 416 * @param off Offset from which to start writing characters
393 417 * @param len Number of characters to write
394 418 */
395 419 public void write(char buf[], int off, int len) {
396 420 try {
397 421 synchronized (lock) {
398 422 ensureOpen();
399 423 out.write(buf, off, len);
400 424 }
401 425 }
402 426 catch (InterruptedIOException x) {
403 427 Thread.currentThread().interrupt();
404 428 }
405 429 catch (IOException x) {
406 430 trouble = true;
407 431 }
408 432 }
409 433
410 434 /**
411 435 * Writes an array of characters. This method cannot be inherited from the
412 436 * Writer class because it must suppress I/O exceptions.
413 437 * @param buf Array of characters to be written
414 438 */
415 439 public void write(char buf[]) {
416 440 write(buf, 0, buf.length);
417 441 }
418 442
419 443 /**
420 444 * Writes a portion of a string.
421 445 * @param s A String
422 446 * @param off Offset from which to start writing characters
423 447 * @param len Number of characters to write
424 448 */
425 449 public void write(String s, int off, int len) {
426 450 try {
427 451 synchronized (lock) {
428 452 ensureOpen();
429 453 out.write(s, off, len);
430 454 }
431 455 }
432 456 catch (InterruptedIOException x) {
433 457 Thread.currentThread().interrupt();
434 458 }
435 459 catch (IOException x) {
436 460 trouble = true;
437 461 }
438 462 }
439 463
440 464 /**
441 465 * Writes a string. This method cannot be inherited from the Writer class
442 466 * because it must suppress I/O exceptions.
443 467 * @param s String to be written
444 468 */
445 469 public void write(String s) {
446 470 write(s, 0, s.length());
447 471 }
448 472
449 473 private void newLine() {
450 474 try {
451 475 synchronized (lock) {
452 476 ensureOpen();
453 477 out.write(lineSeparator);
454 478 if (autoFlush)
455 479 out.flush();
456 480 }
457 481 }
458 482 catch (InterruptedIOException x) {
459 483 Thread.currentThread().interrupt();
460 484 }
461 485 catch (IOException x) {
462 486 trouble = true;
463 487 }
464 488 }
465 489
466 490 /* Methods that do not terminate lines */
467 491
468 492 /**
469 493 * Prints a boolean value. The string produced by <code>{@link
470 494 * java.lang.String#valueOf(boolean)}</code> is translated into bytes
471 495 * according to the platform's default character encoding, and these bytes
472 496 * are written in exactly the manner of the <code>{@link
473 497 * #write(int)}</code> method.
474 498 *
475 499 * @param b The <code>boolean</code> to be printed
476 500 */
477 501 public void print(boolean b) {
478 502 write(b ? "true" : "false");
479 503 }
480 504
481 505 /**
482 506 * Prints a character. The character is translated into one or more bytes
483 507 * according to the platform's default character encoding, and these bytes
484 508 * are written in exactly the manner of the <code>{@link
485 509 * #write(int)}</code> method.
486 510 *
487 511 * @param c The <code>char</code> to be printed
488 512 */
489 513 public void print(char c) {
490 514 write(c);
491 515 }
492 516
493 517 /**
494 518 * Prints an integer. The string produced by <code>{@link
495 519 * java.lang.String#valueOf(int)}</code> is translated into bytes according
496 520 * to the platform's default character encoding, and these bytes are
497 521 * written in exactly the manner of the <code>{@link #write(int)}</code>
498 522 * method.
499 523 *
500 524 * @param i The <code>int</code> to be printed
501 525 * @see java.lang.Integer#toString(int)
502 526 */
503 527 public void print(int i) {
504 528 write(String.valueOf(i));
505 529 }
506 530
507 531 /**
508 532 * Prints a long integer. The string produced by <code>{@link
509 533 * java.lang.String#valueOf(long)}</code> is translated into bytes
510 534 * according to the platform's default character encoding, and these bytes
511 535 * are written in exactly the manner of the <code>{@link #write(int)}</code>
512 536 * method.
513 537 *
514 538 * @param l The <code>long</code> to be printed
515 539 * @see java.lang.Long#toString(long)
516 540 */
517 541 public void print(long l) {
518 542 write(String.valueOf(l));
519 543 }
520 544
521 545 /**
522 546 * Prints a floating-point number. The string produced by <code>{@link
523 547 * java.lang.String#valueOf(float)}</code> is translated into bytes
524 548 * according to the platform's default character encoding, and these bytes
525 549 * are written in exactly the manner of the <code>{@link #write(int)}</code>
526 550 * method.
527 551 *
528 552 * @param f The <code>float</code> to be printed
529 553 * @see java.lang.Float#toString(float)
530 554 */
531 555 public void print(float f) {
532 556 write(String.valueOf(f));
533 557 }
534 558
535 559 /**
536 560 * Prints a double-precision floating-point number. The string produced by
537 561 * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
538 562 * bytes according to the platform's default character encoding, and these
539 563 * bytes are written in exactly the manner of the <code>{@link
540 564 * #write(int)}</code> method.
541 565 *
542 566 * @param d The <code>double</code> to be printed
543 567 * @see java.lang.Double#toString(double)
544 568 */
545 569 public void print(double d) {
546 570 write(String.valueOf(d));
547 571 }
548 572
549 573 /**
550 574 * Prints an array of characters. The characters are converted into bytes
551 575 * according to the platform's default character encoding, and these bytes
552 576 * are written in exactly the manner of the <code>{@link #write(int)}</code>
553 577 * method.
554 578 *
555 579 * @param s The array of chars to be printed
556 580 *
557 581 * @throws NullPointerException If <code>s</code> is <code>null</code>
558 582 */
559 583 public void print(char s[]) {
560 584 write(s);
561 585 }
562 586
563 587 /**
564 588 * Prints a string. If the argument is <code>null</code> then the string
565 589 * <code>"null"</code> is printed. Otherwise, the string's characters are
566 590 * converted into bytes according to the platform's default character
567 591 * encoding, and these bytes are written in exactly the manner of the
568 592 * <code>{@link #write(int)}</code> method.
569 593 *
570 594 * @param s The <code>String</code> to be printed
571 595 */
572 596 public void print(String s) {
573 597 if (s == null) {
574 598 s = "null";
575 599 }
576 600 write(s);
577 601 }
578 602
579 603 /**
580 604 * Prints an object. The string produced by the <code>{@link
581 605 * java.lang.String#valueOf(Object)}</code> method is translated into bytes
582 606 * according to the platform's default character encoding, and these bytes
583 607 * are written in exactly the manner of the <code>{@link #write(int)}</code>
584 608 * method.
585 609 *
586 610 * @param obj The <code>Object</code> to be printed
587 611 * @see java.lang.Object#toString()
588 612 */
589 613 public void print(Object obj) {
590 614 write(String.valueOf(obj));
591 615 }
592 616
593 617 /* Methods that do terminate lines */
594 618
595 619 /**
596 620 * Terminates the current line by writing the line separator string. The
597 621 * line separator string is defined by the system property
598 622 * <code>line.separator</code>, and is not necessarily a single newline
599 623 * character (<code>'\n'</code>).
600 624 */
601 625 public void println() {
602 626 newLine();
603 627 }
604 628
605 629 /**
606 630 * Prints a boolean value and then terminates the line. This method behaves
607 631 * as though it invokes <code>{@link #print(boolean)}</code> and then
608 632 * <code>{@link #println()}</code>.
609 633 *
610 634 * @param x the <code>boolean</code> value to be printed
611 635 */
612 636 public void println(boolean x) {
613 637 synchronized (lock) {
614 638 print(x);
615 639 println();
616 640 }
617 641 }
618 642
619 643 /**
620 644 * Prints a character and then terminates the line. This method behaves as
621 645 * though it invokes <code>{@link #print(char)}</code> and then <code>{@link
622 646 * #println()}</code>.
623 647 *
624 648 * @param x the <code>char</code> value to be printed
625 649 */
626 650 public void println(char x) {
627 651 synchronized (lock) {
628 652 print(x);
629 653 println();
630 654 }
631 655 }
632 656
633 657 /**
634 658 * Prints an integer and then terminates the line. This method behaves as
635 659 * though it invokes <code>{@link #print(int)}</code> and then <code>{@link
636 660 * #println()}</code>.
637 661 *
638 662 * @param x the <code>int</code> value to be printed
639 663 */
640 664 public void println(int x) {
641 665 synchronized (lock) {
642 666 print(x);
643 667 println();
644 668 }
645 669 }
646 670
647 671 /**
648 672 * Prints a long integer and then terminates the line. This method behaves
649 673 * as though it invokes <code>{@link #print(long)}</code> and then
650 674 * <code>{@link #println()}</code>.
651 675 *
652 676 * @param x the <code>long</code> value to be printed
653 677 */
654 678 public void println(long x) {
655 679 synchronized (lock) {
656 680 print(x);
657 681 println();
658 682 }
659 683 }
660 684
661 685 /**
662 686 * Prints a floating-point number and then terminates the line. This method
663 687 * behaves as though it invokes <code>{@link #print(float)}</code> and then
664 688 * <code>{@link #println()}</code>.
665 689 *
666 690 * @param x the <code>float</code> value to be printed
667 691 */
668 692 public void println(float x) {
669 693 synchronized (lock) {
670 694 print(x);
671 695 println();
672 696 }
673 697 }
674 698
675 699 /**
676 700 * Prints a double-precision floating-point number and then terminates the
677 701 * line. This method behaves as though it invokes <code>{@link
678 702 * #print(double)}</code> and then <code>{@link #println()}</code>.
679 703 *
680 704 * @param x the <code>double</code> value to be printed
681 705 */
682 706 public void println(double x) {
683 707 synchronized (lock) {
684 708 print(x);
685 709 println();
686 710 }
687 711 }
688 712
689 713 /**
690 714 * Prints an array of characters and then terminates the line. This method
691 715 * behaves as though it invokes <code>{@link #print(char[])}</code> and then
692 716 * <code>{@link #println()}</code>.
693 717 *
694 718 * @param x the array of <code>char</code> values to be printed
695 719 */
696 720 public void println(char x[]) {
697 721 synchronized (lock) {
698 722 print(x);
699 723 println();
700 724 }
701 725 }
702 726
703 727 /**
704 728 * Prints a String and then terminates the line. This method behaves as
705 729 * though it invokes <code>{@link #print(String)}</code> and then
706 730 * <code>{@link #println()}</code>.
707 731 *
708 732 * @param x the <code>String</code> value to be printed
709 733 */
710 734 public void println(String x) {
711 735 synchronized (lock) {
712 736 print(x);
713 737 println();
714 738 }
715 739 }
716 740
717 741 /**
718 742 * Prints an Object and then terminates the line. This method calls
719 743 * at first String.valueOf(x) to get the printed object's string value,
720 744 * then behaves as
721 745 * though it invokes <code>{@link #print(String)}</code> and then
722 746 * <code>{@link #println()}</code>.
723 747 *
724 748 * @param x The <code>Object</code> to be printed.
725 749 */
726 750 public void println(Object x) {
727 751 String s = String.valueOf(x);
728 752 synchronized (lock) {
729 753 print(s);
730 754 println();
731 755 }
732 756 }
733 757
734 758 /**
735 759 * A convenience method to write a formatted string to this writer using
736 760 * the specified format string and arguments. If automatic flushing is
737 761 * enabled, calls to this method will flush the output buffer.
738 762 *
739 763 * <p> An invocation of this method of the form <tt>out.printf(format,
740 764 * args)</tt> behaves in exactly the same way as the invocation
741 765 *
742 766 * <pre>
743 767 * out.format(format, args) </pre>
744 768 *
745 769 * @param format
746 770 * A format string as described in <a
747 771 * href="../util/Formatter.html#syntax">Format string syntax</a>.
748 772 *
749 773 * @param args
750 774 * Arguments referenced by the format specifiers in the format
751 775 * string. If there are more arguments than format specifiers, the
752 776 * extra arguments are ignored. The number of arguments is
753 777 * variable and may be zero. The maximum number of arguments is
754 778 * limited by the maximum dimension of a Java array as defined by
755 779 * the <a href="http://java.sun.com/docs/books/vmspec/">Java
756 780 * Virtual Machine Specification</a>. The behaviour on a
757 781 * <tt>null</tt> argument depends on the <a
758 782 * href="../util/Formatter.html#syntax">conversion</a>.
759 783 *
760 784 * @throws IllegalFormatException
761 785 * If a format string contains an illegal syntax, a format
762 786 * specifier that is incompatible with the given arguments,
763 787 * insufficient arguments given the format string, or other
764 788 * illegal conditions. For specification of all possible
765 789 * formatting errors, see the <a
766 790 * href="../util/Formatter.html#detail">Details</a> section of the
767 791 * formatter class specification.
768 792 *
769 793 * @throws NullPointerException
770 794 * If the <tt>format</tt> is <tt>null</tt>
771 795 *
772 796 * @return This writer
773 797 *
774 798 * @since 1.5
775 799 */
776 800 public PrintWriter printf(String format, Object ... args) {
777 801 return format(format, args);
778 802 }
779 803
780 804 /**
781 805 * A convenience method to write a formatted string to this writer using
782 806 * the specified format string and arguments. If automatic flushing is
783 807 * enabled, calls to this method will flush the output buffer.
784 808 *
785 809 * <p> An invocation of this method of the form <tt>out.printf(l, format,
786 810 * args)</tt> behaves in exactly the same way as the invocation
787 811 *
788 812 * <pre>
789 813 * out.format(l, format, args) </pre>
790 814 *
791 815 * @param l
792 816 * The {@linkplain java.util.Locale locale} to apply during
793 817 * formatting. If <tt>l</tt> is <tt>null</tt> then no localization
794 818 * is applied.
795 819 *
796 820 * @param format
797 821 * A format string as described in <a
798 822 * href="../util/Formatter.html#syntax">Format string syntax</a>.
799 823 *
800 824 * @param args
801 825 * Arguments referenced by the format specifiers in the format
802 826 * string. If there are more arguments than format specifiers, the
803 827 * extra arguments are ignored. The number of arguments is
804 828 * variable and may be zero. The maximum number of arguments is
805 829 * limited by the maximum dimension of a Java array as defined by
806 830 * the <a href="http://java.sun.com/docs/books/vmspec/">Java
807 831 * Virtual Machine Specification</a>. The behaviour on a
808 832 * <tt>null</tt> argument depends on the <a
809 833 * href="../util/Formatter.html#syntax">conversion</a>.
810 834 *
811 835 * @throws IllegalFormatException
812 836 * If a format string contains an illegal syntax, a format
813 837 * specifier that is incompatible with the given arguments,
814 838 * insufficient arguments given the format string, or other
815 839 * illegal conditions. For specification of all possible
816 840 * formatting errors, see the <a
817 841 * href="../util/Formatter.html#detail">Details</a> section of the
818 842 * formatter class specification.
819 843 *
820 844 * @throws NullPointerException
821 845 * If the <tt>format</tt> is <tt>null</tt>
822 846 *
823 847 * @return This writer
824 848 *
825 849 * @since 1.5
826 850 */
827 851 public PrintWriter printf(Locale l, String format, Object ... args) {
828 852 return format(l, format, args);
829 853 }
830 854
831 855 /**
832 856 * Writes a formatted string to this writer using the specified format
833 857 * string and arguments. If automatic flushing is enabled, calls to this
834 858 * method will flush the output buffer.
835 859 *
836 860 * <p> The locale always used is the one returned by {@link
837 861 * java.util.Locale#getDefault() Locale.getDefault()}, regardless of any
838 862 * previous invocations of other formatting methods on this object.
839 863 *
840 864 * @param format
841 865 * A format string as described in <a
842 866 * href="../util/Formatter.html#syntax">Format string syntax</a>.
843 867 *
844 868 * @param args
845 869 * Arguments referenced by the format specifiers in the format
846 870 * string. If there are more arguments than format specifiers, the
847 871 * extra arguments are ignored. The number of arguments is
848 872 * variable and may be zero. The maximum number of arguments is
849 873 * limited by the maximum dimension of a Java array as defined by
850 874 * the <a href="http://java.sun.com/docs/books/vmspec/">Java
851 875 * Virtual Machine Specification</a>. The behaviour on a
852 876 * <tt>null</tt> argument depends on the <a
853 877 * href="../util/Formatter.html#syntax">conversion</a>.
854 878 *
855 879 * @throws IllegalFormatException
856 880 * If a format string contains an illegal syntax, a format
857 881 * specifier that is incompatible with the given arguments,
858 882 * insufficient arguments given the format string, or other
859 883 * illegal conditions. For specification of all possible
860 884 * formatting errors, see the <a
861 885 * href="../util/Formatter.html#detail">Details</a> section of the
862 886 * Formatter class specification.
863 887 *
864 888 * @throws NullPointerException
865 889 * If the <tt>format</tt> is <tt>null</tt>
866 890 *
867 891 * @return This writer
868 892 *
869 893 * @since 1.5
870 894 */
871 895 public PrintWriter format(String format, Object ... args) {
872 896 try {
873 897 synchronized (lock) {
874 898 ensureOpen();
875 899 if ((formatter == null)
876 900 || (formatter.locale() != Locale.getDefault()))
877 901 formatter = new Formatter(this);
878 902 formatter.format(Locale.getDefault(), format, args);
879 903 if (autoFlush)
880 904 out.flush();
881 905 }
882 906 } catch (InterruptedIOException x) {
883 907 Thread.currentThread().interrupt();
884 908 } catch (IOException x) {
885 909 trouble = true;
886 910 }
887 911 return this;
888 912 }
889 913
890 914 /**
891 915 * Writes a formatted string to this writer using the specified format
892 916 * string and arguments. If automatic flushing is enabled, calls to this
893 917 * method will flush the output buffer.
894 918 *
895 919 * @param l
896 920 * The {@linkplain java.util.Locale locale} to apply during
897 921 * formatting. If <tt>l</tt> is <tt>null</tt> then no localization
898 922 * is applied.
899 923 *
900 924 * @param format
901 925 * A format string as described in <a
902 926 * href="../util/Formatter.html#syntax">Format string syntax</a>.
903 927 *
904 928 * @param args
905 929 * Arguments referenced by the format specifiers in the format
906 930 * string. If there are more arguments than format specifiers, the
907 931 * extra arguments are ignored. The number of arguments is
908 932 * variable and may be zero. The maximum number of arguments is
909 933 * limited by the maximum dimension of a Java array as defined by
910 934 * the <a href="http://java.sun.com/docs/books/vmspec/">Java
911 935 * Virtual Machine Specification</a>. The behaviour on a
912 936 * <tt>null</tt> argument depends on the <a
913 937 * href="../util/Formatter.html#syntax">conversion</a>.
914 938 *
915 939 * @throws IllegalFormatException
916 940 * If a format string contains an illegal syntax, a format
917 941 * specifier that is incompatible with the given arguments,
918 942 * insufficient arguments given the format string, or other
919 943 * illegal conditions. For specification of all possible
920 944 * formatting errors, see the <a
921 945 * href="../util/Formatter.html#detail">Details</a> section of the
922 946 * formatter class specification.
923 947 *
924 948 * @throws NullPointerException
925 949 * If the <tt>format</tt> is <tt>null</tt>
926 950 *
927 951 * @return This writer
928 952 *
929 953 * @since 1.5
930 954 */
931 955 public PrintWriter format(Locale l, String format, Object ... args) {
932 956 try {
933 957 synchronized (lock) {
934 958 ensureOpen();
935 959 if ((formatter == null) || (formatter.locale() != l))
936 960 formatter = new Formatter(this, l);
937 961 formatter.format(l, format, args);
938 962 if (autoFlush)
939 963 out.flush();
940 964 }
941 965 } catch (InterruptedIOException x) {
942 966 Thread.currentThread().interrupt();
943 967 } catch (IOException x) {
944 968 trouble = true;
945 969 }
946 970 return this;
947 971 }
948 972
949 973 /**
950 974 * Appends the specified character sequence to this writer.
951 975 *
952 976 * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
953 977 * behaves in exactly the same way as the invocation
954 978 *
955 979 * <pre>
956 980 * out.write(csq.toString()) </pre>
957 981 *
958 982 * <p> Depending on the specification of <tt>toString</tt> for the
959 983 * character sequence <tt>csq</tt>, the entire sequence may not be
960 984 * appended. For instance, invoking the <tt>toString</tt> method of a
961 985 * character buffer will return a subsequence whose content depends upon
962 986 * the buffer's position and limit.
963 987 *
964 988 * @param csq
965 989 * The character sequence to append. If <tt>csq</tt> is
966 990 * <tt>null</tt>, then the four characters <tt>"null"</tt> are
967 991 * appended to this writer.
968 992 *
969 993 * @return This writer
970 994 *
971 995 * @since 1.5
972 996 */
973 997 public PrintWriter append(CharSequence csq) {
974 998 if (csq == null)
975 999 write("null");
976 1000 else
977 1001 write(csq.toString());
978 1002 return this;
979 1003 }
980 1004
981 1005 /**
982 1006 * Appends a subsequence of the specified character sequence to this writer.
983 1007 *
984 1008 * <p> An invocation of this method of the form <tt>out.append(csq, start,
985 1009 * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
986 1010 * exactly the same way as the invocation
987 1011 *
988 1012 * <pre>
989 1013 * out.write(csq.subSequence(start, end).toString()) </pre>
990 1014 *
991 1015 * @param csq
992 1016 * The character sequence from which a subsequence will be
993 1017 * appended. If <tt>csq</tt> is <tt>null</tt>, then characters
994 1018 * will be appended as if <tt>csq</tt> contained the four
995 1019 * characters <tt>"null"</tt>.
996 1020 *
997 1021 * @param start
998 1022 * The index of the first character in the subsequence
999 1023 *
1000 1024 * @param end
1001 1025 * The index of the character following the last character in the
1002 1026 * subsequence
1003 1027 *
1004 1028 * @return This writer
1005 1029 *
1006 1030 * @throws IndexOutOfBoundsException
1007 1031 * If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
1008 1032 * is greater than <tt>end</tt>, or <tt>end</tt> is greater than
1009 1033 * <tt>csq.length()</tt>
1010 1034 *
1011 1035 * @since 1.5
1012 1036 */
1013 1037 public PrintWriter append(CharSequence csq, int start, int end) {
1014 1038 CharSequence cs = (csq == null ? "null" : csq);
1015 1039 write(cs.subSequence(start, end).toString());
1016 1040 return this;
1017 1041 }
1018 1042
1019 1043 /**
1020 1044 * Appends the specified character to this writer.
1021 1045 *
1022 1046 * <p> An invocation of this method of the form <tt>out.append(c)</tt>
1023 1047 * behaves in exactly the same way as the invocation
1024 1048 *
1025 1049 * <pre>
1026 1050 * out.write(c) </pre>
1027 1051 *
1028 1052 * @param c
1029 1053 * The 16-bit character to append
1030 1054 *
1031 1055 * @return This writer
1032 1056 *
1033 1057 * @since 1.5
1034 1058 */
1035 1059 public PrintWriter append(char c) {
1036 1060 write(c);
1037 1061 return this;
1038 1062 }
1039 1063 }
↓ open down ↓ |
753 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX