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