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