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