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