PrintWriter.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /* PrintWriter.java -- prints primitive values and objects to a stream as text
  2. Copyright (C) 1998, 1999, 2000, 2001, 2005 Free Software Foundation
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.io;
  32. import java.util.Locale;
  33. import java.util.Formatter;
  34. /* Written using "Java Class Libraries", 2nd edition, plus online
  35. * API docs for JDK 1.2 beta from http://www.javasoft.com.
  36. * Status: Believed complete and correct.
  37. * However, should use native methods for conversion.
  38. */
  39. /**
  40. * This class prints Java primitive values and objects to a stream as
  41. * text. None of the methods in this class throw an exception. However,
  42. * errors can be detected by calling the <code>checkError()</code> method.
  43. * Additionally, this stream can be designated as "autoflush" when
  44. * created so that any writes are automatically flushed to the underlying
  45. * output sink whenever one of the <code>println</code> methods is
  46. * called. (Note that this differs from the <code>PrintStream</code>
  47. * class which also auto-flushes when it encounters a newline character
  48. * in the chars written).
  49. *
  50. * @author Per Bothner (bothner@cygnus.com)
  51. * @author Aaron M. Renn (arenn@urbanophile.com)
  52. * @date April 17, 1998.
  53. */
  54. public class PrintWriter extends Writer
  55. {
  56. /**
  57. * <code>true</code> if auto-flush is enabled, <code>false</code> otherwise
  58. */
  59. private boolean autoflush;
  60. /**
  61. * This boolean indicates whether or not an error has ever occurred
  62. * on this stream.
  63. */
  64. private boolean error;
  65. /**
  66. * Indicates whether or not the stream has been closed.
  67. */
  68. private boolean closed;
  69. /**
  70. * This is the underlying <code>Writer</code> we are sending output
  71. * to
  72. */
  73. protected Writer out;
  74. /**
  75. * This method intializes a new <code>PrintWriter</code> object to write
  76. * to the specified output sink. The form of the constructor does not
  77. * enable auto-flush functionality.
  78. *
  79. * @param wr The <code>Writer</code> to write to.
  80. */
  81. public PrintWriter(Writer wr)
  82. {
  83. super(wr.lock);
  84. this.out = wr;
  85. }
  86. /**
  87. * This method intializes a new <code>PrintWriter</code> object to write
  88. * to the specified output sink. This constructor also allows "auto-flush"
  89. * functionality to be specified where the stream will be flushed after
  90. * every line is terminated or newline character is written.
  91. *
  92. * @param wr The <code>Writer</code> to write to.
  93. * @param autoflush <code>true</code> to flush the stream after every
  94. * line, <code>false</code> otherwise
  95. */
  96. public PrintWriter(Writer wr, boolean autoflush)
  97. {
  98. super(wr.lock);
  99. this.out = wr;
  100. this.autoflush = autoflush;
  101. }
  102. /**
  103. * This method initializes a new <code>PrintWriter</code> object to write
  104. * to the specified <code>OutputStream</code>. Characters will be converted
  105. * to chars using the system default encoding. Auto-flush functionality
  106. * will not be enabled.
  107. *
  108. * @param out The <code>OutputStream</code> to write to
  109. */
  110. public PrintWriter(OutputStream out)
  111. {
  112. super();
  113. this.out = new OutputStreamWriter(out);
  114. this.lock = this.out;
  115. }
  116. /**
  117. * This method initializes a new <code>PrintWriter</code> object to write
  118. * to the specified <code>OutputStream</code>. Characters will be converted
  119. * to chars using the system default encoding. This form of the
  120. * constructor allows auto-flush functionality to be enabled if desired
  121. *
  122. * @param out The <code>OutputStream</code> to write to
  123. * @param autoflush <code>true</code> to flush the stream after every
  124. * <code>println</code> call, <code>false</code> otherwise.
  125. */
  126. public PrintWriter(OutputStream out, boolean autoflush)
  127. {
  128. this(out);
  129. this.autoflush = autoflush;
  130. }
  131. /**
  132. * This initializes a new PrintWriter object to write to the specified
  133. * file. It creates a FileOutputStream object and wraps it in an
  134. * OutputStreamWriter using the default encoding.
  135. * @param file name of the file to write to
  136. * @throws FileNotFoundException if the file cannot be written or created
  137. *
  138. * @since 1.5
  139. */
  140. public PrintWriter(String file) throws FileNotFoundException
  141. {
  142. this(new FileOutputStream(file));
  143. }
  144. /**
  145. * This initializes a new PrintWriter object to write to the specified
  146. * file. It creates a FileOutputStream object and wraps it in an
  147. * OutputStreamWriter using the specified encoding.
  148. * @param file name of the file to write to
  149. * @param enc the encoding to use
  150. * @throws FileNotFoundException if the file cannot be written or created
  151. * @throws UnsupportedEncodingException if the encoding is not supported
  152. *
  153. * @since 1.5
  154. */
  155. public PrintWriter(String file, String enc)
  156. throws FileNotFoundException, UnsupportedEncodingException
  157. {
  158. this(new OutputStreamWriter(new FileOutputStream(file), enc));
  159. }
  160. /**
  161. * This initializes a new PrintWriter object to write to the specified
  162. * file. It creates a FileOutputStream object and wraps it in an
  163. * OutputStreamWriter using the default encoding.
  164. * @param file the file to write to
  165. * @throws FileNotFoundException if the file cannot be written or created
  166. *
  167. * @since 1.5
  168. */
  169. public PrintWriter(File file) throws FileNotFoundException
  170. {
  171. this(new FileOutputStream(file));
  172. }
  173. /**
  174. * This initializes a new PrintWriter object to write to the specified
  175. * file. It creates a FileOutputStream object and wraps it in an
  176. * OutputStreamWriter using the specified encoding.
  177. * @param file the file to write to
  178. * @param enc the encoding to use
  179. * @throws FileNotFoundException if the file cannot be written or created
  180. * @throws UnsupportedEncodingException if the encoding is not supported
  181. *
  182. * @since 1.5
  183. */
  184. public PrintWriter(File file, String enc)
  185. throws FileNotFoundException, UnsupportedEncodingException
  186. {
  187. this(new OutputStreamWriter(new FileOutputStream(file), enc));
  188. }
  189. /**
  190. * This method can be called by subclasses to indicate that an error
  191. * has occurred and should be reported by <code>checkError</code>.
  192. */
  193. protected void setError()
  194. {
  195. error = true;
  196. }
  197. /**
  198. * This method checks to see if an error has occurred on this stream. Note
  199. * that once an error has occurred, this method will continue to report
  200. * <code>true</code> forever for this stream. Before checking for an
  201. * error condition, this method flushes the stream.
  202. *
  203. * @return <code>true</code> if an error has occurred,
  204. * <code>false</code> otherwise
  205. */
  206. public boolean checkError()
  207. {
  208. if (! closed)
  209. flush();
  210. return error;
  211. }
  212. /**
  213. * This method flushes any buffered chars to the underlying stream and
  214. * then flushes that stream as well.
  215. */
  216. public void flush()
  217. {
  218. try
  219. {
  220. out.flush();
  221. }
  222. catch (IOException ex)
  223. {
  224. error = true;
  225. }
  226. }
  227. /**
  228. * This method closes this stream and all underlying streams.
  229. */
  230. public void close()
  231. {
  232. try
  233. {
  234. out.close();
  235. closed = true;
  236. }
  237. catch (IOException ex)
  238. {
  239. error = true;
  240. }
  241. }
  242. /**
  243. * This method prints a <code>String</code> to the stream. The actual
  244. * value printed depends on the system default encoding.
  245. *
  246. * @param str The <code>String</code> to print.
  247. */
  248. public void print(String str)
  249. {
  250. write(str == null ? "null" : str);
  251. }
  252. /**
  253. * This method prints a char to the stream. The actual value printed is
  254. * determined by the character encoding in use.
  255. *
  256. * @param ch The <code>char</code> value to be printed
  257. */
  258. public void print(char ch)
  259. {
  260. write((int) ch);
  261. }
  262. /**
  263. * This method prints an array of characters to the stream. The actual
  264. * value printed depends on the system default encoding.
  265. *
  266. * @param charArray The array of characters to print.
  267. */
  268. public void print(char[] charArray)
  269. {
  270. write(charArray, 0, charArray.length);
  271. }
  272. /**
  273. * This methods prints a boolean value to the stream. <code>true</code>
  274. * values are printed as "true" and <code>false</code> values are printed
  275. * as "false".
  276. *
  277. * @param bool The <code>boolean</code> value to print
  278. */
  279. public void print(boolean bool)
  280. {
  281. // We purposely call write() and not print() here. This preserves
  282. // compatibility with JDK 1.2.
  283. write (bool ? "true" : "false");
  284. }
  285. /**
  286. * This method prints an integer to the stream. The value printed is
  287. * determined using the <code>String.valueOf()</code> method.
  288. *
  289. * @param inum The <code>int</code> value to be printed
  290. */
  291. public void print(int inum)
  292. {
  293. // We purposely call write() and not print() here. This preserves
  294. // compatibility with JDK 1.2.
  295. write(Integer.toString(inum));
  296. }
  297. /**
  298. * This method prints a long to the stream. The value printed is
  299. * determined using the <code>String.valueOf()</code> method.
  300. *
  301. * @param lnum The <code>long</code> value to be printed
  302. */
  303. public void print(long lnum)
  304. {
  305. // We purposely call write() and not print() here. This preserves
  306. // compatibility with JDK 1.2.
  307. write(Long.toString(lnum));
  308. }
  309. /**
  310. * This method prints a float to the stream. The value printed is
  311. * determined using the <code>String.valueOf()</code> method.
  312. *
  313. * @param fnum The <code>float</code> value to be printed
  314. */
  315. public void print(float fnum)
  316. {
  317. // We purposely call write() and not print() here. This preserves
  318. // compatibility with JDK 1.2.
  319. write(Float.toString(fnum));
  320. }
  321. /**
  322. * This method prints a double to the stream. The value printed is
  323. * determined using the <code>String.valueOf()</code> method.
  324. *
  325. * @param dnum The <code>double</code> value to be printed
  326. */
  327. public void print(double dnum)
  328. {
  329. // We purposely call write() and not print() here. This preserves
  330. // compatibility with JDK 1.2.
  331. write(Double.toString(dnum));
  332. }
  333. /**
  334. * This method prints an <code>Object</code> to the stream. The actual
  335. * value printed is determined by calling the <code>String.valueOf()</code>
  336. * method.
  337. *
  338. * @param obj The <code>Object</code> to print.
  339. */
  340. public void print(Object obj)
  341. {
  342. // We purposely call write() and not print() here. This preserves
  343. // compatibility with JDK 1.2.
  344. write(obj == null ? "null" : obj.toString());
  345. }
  346. /**
  347. * This is the system dependent line separator
  348. */
  349. private static final char[] line_separator
  350. = System.getProperty("line.separator", "\n").toCharArray();
  351. /**
  352. * This method prints a line separator sequence to the stream. The value
  353. * printed is determined by the system property <xmp>line.separator</xmp>
  354. * and is not necessarily the Unix '\n' newline character.
  355. */
  356. public void println()
  357. {
  358. synchronized (lock)
  359. {
  360. try
  361. {
  362. write(line_separator, 0, line_separator.length);
  363. if (autoflush)
  364. out.flush();
  365. }
  366. catch (IOException ex)
  367. {
  368. error = true;
  369. }
  370. }
  371. }
  372. /**
  373. * This methods prints a boolean value to the stream. <code>true</code>
  374. * values are printed as "true" and <code>false</code> values are printed
  375. * as "false".
  376. *
  377. * This method prints a line termination sequence after printing the value.
  378. *
  379. * @param bool The <code>boolean</code> value to print
  380. */
  381. public void println(boolean bool)
  382. {
  383. synchronized (lock)
  384. {
  385. print(bool);
  386. println();
  387. }
  388. }
  389. /**
  390. * This method prints an integer to the stream. The value printed is
  391. * determined using the <code>String.valueOf()</code> method.
  392. *
  393. * This method prints a line termination sequence after printing the value.
  394. *
  395. * @param inum The <code>int</code> value to be printed
  396. */
  397. public void println(int inum)
  398. {
  399. synchronized (lock)
  400. {
  401. print(inum);
  402. println();
  403. }
  404. }
  405. /**
  406. * This method prints a long to the stream. The value printed is
  407. * determined using the <code>String.valueOf()</code> method.
  408. *
  409. * This method prints a line termination sequence after printing the value.
  410. *
  411. * @param lnum The <code>long</code> value to be printed
  412. */
  413. public void println(long lnum)
  414. {
  415. synchronized (lock)
  416. {
  417. print(lnum);
  418. println();
  419. }
  420. }
  421. /**
  422. * This method prints a float to the stream. The value printed is
  423. * determined using the <code>String.valueOf()</code> method.
  424. *
  425. * This method prints a line termination sequence after printing the value.
  426. *
  427. * @param fnum The <code>float</code> value to be printed
  428. */
  429. public void println(float fnum)
  430. {
  431. synchronized (lock)
  432. {
  433. print(fnum);
  434. println();
  435. }
  436. }
  437. /**
  438. * This method prints a double to the stream. The value printed is
  439. * determined using the <code>String.valueOf()</code> method.
  440. *
  441. * This method prints a line termination sequence after printing the value.
  442. *
  443. * @param dnum The <code>double</code> value to be printed
  444. */
  445. public void println(double dnum)
  446. {
  447. synchronized (lock)
  448. {
  449. print(dnum);
  450. println();
  451. }
  452. }
  453. /**
  454. * This method prints an <code>Object</code> to the stream. The actual
  455. * value printed is determined by calling the <code>String.valueOf()</code>
  456. * method.
  457. *
  458. * This method prints a line termination sequence after printing the value.
  459. *
  460. * @param obj The <code>Object</code> to print.
  461. */
  462. public void println(Object obj)
  463. {
  464. synchronized (lock)
  465. {
  466. print(obj);
  467. println();
  468. }
  469. }
  470. /**
  471. * This method prints a <code>String</code> to the stream. The actual
  472. * value printed depends on the system default encoding.
  473. *
  474. * This method prints a line termination sequence after printing the value.
  475. *
  476. * @param str The <code>String</code> to print.
  477. */
  478. public void println(String str)
  479. {
  480. synchronized (lock)
  481. {
  482. print(str);
  483. println();
  484. }
  485. }
  486. /**
  487. * This method prints a char to the stream. The actual value printed is
  488. * determined by the character encoding in use.
  489. *
  490. * This method prints a line termination sequence after printing the value.
  491. *
  492. * @param ch The <code>char</code> value to be printed
  493. */
  494. public void println(char ch)
  495. {
  496. synchronized (lock)
  497. {
  498. print(ch);
  499. println();
  500. }
  501. }
  502. /**
  503. * This method prints an array of characters to the stream. The actual
  504. * value printed depends on the system default encoding.
  505. *
  506. * This method prints a line termination sequence after printing the value.
  507. *
  508. * @param charArray The array of characters to print.
  509. */
  510. public void println(char[] charArray)
  511. {
  512. synchronized (lock)
  513. {
  514. print(charArray);
  515. println();
  516. }
  517. }
  518. /**
  519. * This method writes a single char to the stream.
  520. *
  521. * @param ch The char to be written, passed as a int
  522. */
  523. public void write(int ch)
  524. {
  525. try
  526. {
  527. out.write(ch);
  528. }
  529. catch (IOException ex)
  530. {
  531. error = true;
  532. }
  533. }
  534. /**
  535. * This method writes <code>count</code> chars from the specified array
  536. * starting at index <code>offset</code> into the array.
  537. *
  538. * @param charArray The array of chars to write
  539. * @param offset The index into the array to start writing from
  540. * @param count The number of chars to write
  541. */
  542. public void write(char[] charArray, int offset, int count)
  543. {
  544. try
  545. {
  546. out.write(charArray, offset, count);
  547. }
  548. catch (IOException ex)
  549. {
  550. error = true;
  551. }
  552. }
  553. /**
  554. * This method writes <code>count</code> chars from the specified
  555. * <code>String</code> to the output starting at character position
  556. * <code>offset</code> into the <code>String</code>
  557. *
  558. * @param str The <code>String</code> to write chars from
  559. * @param offset The offset into the <code>String</code> to start writing from
  560. * @param count The number of chars to write.
  561. */
  562. public void write(String str, int offset, int count)
  563. {
  564. try
  565. {
  566. out.write(str, offset, count);
  567. }
  568. catch (IOException ex)
  569. {
  570. error = true;
  571. }
  572. }
  573. /**
  574. * This method write all the chars in the specified array to the output.
  575. *
  576. * @param charArray The array of characters to write
  577. */
  578. public void write(char[] charArray)
  579. {
  580. write(charArray, 0, charArray.length);
  581. }
  582. /**
  583. * This method writes the contents of the specified <code>String</code>
  584. * to the underlying stream.
  585. *
  586. * @param str The <code>String</code> to write
  587. */
  588. public void write(String str)
  589. {
  590. write(str, 0, str.length());
  591. }
  592. /** @since 1.5 */
  593. public PrintWriter append(char c)
  594. {
  595. write(c);
  596. return this;
  597. }
  598. /** @since 1.5 */
  599. public PrintWriter append(CharSequence cs)
  600. {
  601. write(cs == null ? "null" : cs.toString());
  602. return this;
  603. }
  604. /** @since 1.5 */
  605. public PrintWriter append(CharSequence cs, int start, int end)
  606. {
  607. write(cs == null ? "null" : cs.subSequence(start, end).toString());
  608. return this;
  609. }
  610. /** @since 1.5 */
  611. public PrintWriter printf(String format, Object... args)
  612. {
  613. return format(format, args);
  614. }
  615. /** @since 1.5 */
  616. public PrintWriter printf(Locale locale, String format, Object... args)
  617. {
  618. return format(locale, format, args);
  619. }
  620. /** @since 1.5 */
  621. public PrintWriter format(String format, Object... args)
  622. {
  623. return format(Locale.getDefault(), format, args);
  624. }
  625. /** @since 1.5 */
  626. public PrintWriter format(Locale locale, String format, Object... args)
  627. {
  628. Formatter f = new Formatter(this, locale);
  629. f.format(format, args);
  630. return this;
  631. }
  632. }