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