1. /*
  2. * @(#)PrintWriter.java 1.24 00/02/02
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.io;
  11. /**
  12. * Print formatted representations of objects to a text-output stream. This
  13. * class implements all of the print methods found in PrintStream. It does not
  14. * contain methods for writing raw bytes, for which a program should use
  15. * unencoded byte streams.
  16. *
  17. * <p> Unlike the PrintStream class, if automatic flushing is enabled it will
  18. * be done only when one of the println() methods is invoked, rather than
  19. * whenever a newline character happens to be output. The println() methods
  20. * use the platform's own notion of line separator rather than the newline
  21. * character.
  22. *
  23. * <p> Methods in this class never throw I/O exceptions. The client may
  24. * inquire as to whether any errors have occurred by invoking checkError().
  25. *
  26. * @version 1.24, 02/02/00
  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. /**
  42. * Line separator string. This is the value of the line.separator
  43. * property at the moment that the stream was created.
  44. */
  45. private String lineSeparator;
  46. /**
  47. * Create a new PrintWriter, without automatic line flushing.
  48. *
  49. * @param out A character-output stream
  50. */
  51. public PrintWriter (Writer out) {
  52. this(out, false);
  53. }
  54. /**
  55. * Create a new PrintWriter.
  56. *
  57. * @param out A character-output stream
  58. * @param autoFlush A boolean; if true, the println() methods will flush
  59. * the output buffer
  60. */
  61. public PrintWriter(Writer out,
  62. boolean autoFlush) {
  63. super(out);
  64. this.out = out;
  65. this.autoFlush = autoFlush;
  66. lineSeparator = (String) java.security.AccessController.doPrivileged(
  67. new sun.security.action.GetPropertyAction("line.separator"));
  68. }
  69. /**
  70. * Create a new PrintWriter, without automatic line flushing, from an
  71. * existing OutputStream. This convenience constructor creates the
  72. * necessary intermediate OutputStreamWriter, which will convert characters
  73. * into bytes using the default character encoding.
  74. *
  75. * @param out An output stream
  76. *
  77. * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
  78. */
  79. public PrintWriter(OutputStream out) {
  80. this(out, false);
  81. }
  82. /**
  83. * Create a new PrintWriter from an existing OutputStream. This
  84. * convenience constructor creates the necessary intermediate
  85. * OutputStreamWriter, which will convert characters into bytes using the
  86. * default character encoding.
  87. *
  88. * @param out An output stream
  89. * @param autoFlush A boolean; if true, the println() methods will flush
  90. * the output buffer
  91. *
  92. * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
  93. */
  94. public PrintWriter(OutputStream out, boolean autoFlush) {
  95. this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
  96. }
  97. /** Check to make sure that the stream has not been closed */
  98. private void ensureOpen() throws IOException {
  99. if (out == null)
  100. throw new IOException("Stream closed");
  101. }
  102. /** Flush the stream. */
  103. public void flush() {
  104. try {
  105. synchronized (lock) {
  106. ensureOpen();
  107. out.flush();
  108. }
  109. }
  110. catch (IOException x) {
  111. trouble = true;
  112. }
  113. }
  114. /** Close the stream. */
  115. public void close() {
  116. try {
  117. synchronized (lock) {
  118. if (out == null)
  119. return;
  120. out.close();
  121. out = null;
  122. }
  123. }
  124. catch (IOException x) {
  125. trouble = true;
  126. }
  127. }
  128. /**
  129. * Flush the stream and check its error state. Errors are cumulative;
  130. * once the stream encounters an error, this routine will return true on
  131. * all successive calls.
  132. *
  133. * @return True if the print stream has encountered an error, either on the
  134. * underlying output stream or during a format conversion.
  135. */
  136. public boolean checkError() {
  137. if (out != null)
  138. flush();
  139. return trouble;
  140. }
  141. /** Indicate that an error has occurred. */
  142. protected void setError() {
  143. trouble = true;
  144. }
  145. /*
  146. * Exception-catching, synchronized output operations,
  147. * which also implement the write() methods of Writer
  148. */
  149. /** Write a single character. */
  150. public void write(int c) {
  151. try {
  152. synchronized (lock) {
  153. ensureOpen();
  154. out.write(c);
  155. }
  156. }
  157. catch (InterruptedIOException x) {
  158. Thread.currentThread().interrupt();
  159. }
  160. catch (IOException x) {
  161. trouble = true;
  162. }
  163. }
  164. /** Write a portion of an array of characters. */
  165. public void write(char buf[], int off, int len) {
  166. try {
  167. synchronized (lock) {
  168. ensureOpen();
  169. out.write(buf, off, len);
  170. }
  171. }
  172. catch (InterruptedIOException x) {
  173. Thread.currentThread().interrupt();
  174. }
  175. catch (IOException x) {
  176. trouble = true;
  177. }
  178. }
  179. /**
  180. * Write an array of characters. This method cannot be inherited from the
  181. * Writer class because it must suppress I/O exceptions.
  182. */
  183. public void write(char buf[]) {
  184. write(buf, 0, buf.length);
  185. }
  186. /** Write a portion of a string. */
  187. public void write(String s, int off, int len) {
  188. try {
  189. synchronized (lock) {
  190. ensureOpen();
  191. out.write(s, off, len);
  192. }
  193. }
  194. catch (InterruptedIOException x) {
  195. Thread.currentThread().interrupt();
  196. }
  197. catch (IOException x) {
  198. trouble = true;
  199. }
  200. }
  201. /**
  202. * Write a string. This method cannot be inherited from the Writer class
  203. * because it must suppress I/O exceptions.
  204. */
  205. public void write(String s) {
  206. write(s, 0, s.length());
  207. }
  208. private void newLine() {
  209. try {
  210. synchronized (lock) {
  211. ensureOpen();
  212. out.write(lineSeparator);
  213. if (autoFlush)
  214. out.flush();
  215. }
  216. }
  217. catch (InterruptedIOException x) {
  218. Thread.currentThread().interrupt();
  219. }
  220. catch (IOException x) {
  221. trouble = true;
  222. }
  223. }
  224. /* Methods that do not terminate lines */
  225. /**
  226. * Print a boolean value. The string produced by <code>{@link
  227. * java.lang.String#valueOf(boolean)}</code> is translated into bytes
  228. * according to the platform's default character encoding, and these bytes
  229. * are written in exactly the manner of the <code>{@link
  230. * #write(int)}</code> method.
  231. *
  232. * @param b The <code>boolean</code> to be printed
  233. */
  234. public void print(boolean b) {
  235. write(b ? "true" : "false");
  236. }
  237. /**
  238. * Print a character. The character is translated into one or more bytes
  239. * according to the platform's default character encoding, and these bytes
  240. * are written in exactly the manner of the <code>{@link
  241. * #write(int)}</code> method.
  242. *
  243. * @param c The <code>char</code> to be printed
  244. */
  245. public void print(char c) {
  246. write(String.valueOf(c));
  247. }
  248. /**
  249. * Print an integer. The string produced by <code>{@link
  250. * java.lang.String#valueOf(int)}</code> is translated into bytes according
  251. * to the platform's default character encoding, and these bytes are
  252. * written in exactly the manner of the <code>{@link #write(int)}</code>
  253. * method.
  254. *
  255. * @param i The <code>int</code> to be printed
  256. * @see java.lang.Integer#toString(int)
  257. */
  258. public void print(int i) {
  259. write(String.valueOf(i));
  260. }
  261. /**
  262. * Print a long integer. The string produced by <code>{@link
  263. * java.lang.String#valueOf(long)}</code> is translated into bytes
  264. * according to the platform's default character encoding, and these bytes
  265. * are written in exactly the manner of the <code>{@link #write(int)}</code>
  266. * method.
  267. *
  268. * @param l The <code>long</code> to be printed
  269. * @see java.lang.Long#toString(long)
  270. */
  271. public void print(long l) {
  272. write(String.valueOf(l));
  273. }
  274. /**
  275. * Print a floating-point number. The string produced by <code>{@link
  276. * java.lang.String#valueOf(float)}</code> is translated into bytes
  277. * according to the platform's default character encoding, and these bytes
  278. * are written in exactly the manner of the <code>{@link #write(int)}</code>
  279. * method.
  280. *
  281. * @param f The <code>float</code> to be printed
  282. * @see java.lang.Float#toString(float)
  283. */
  284. public void print(float f) {
  285. write(String.valueOf(f));
  286. }
  287. /**
  288. * Print a double-precision floating-point number. The string produced by
  289. * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
  290. * bytes according to the platform's default character encoding, and these
  291. * bytes are written in exactly the manner of the <code>{@link
  292. * #write(int)}</code> method.
  293. *
  294. * @param d The <code>double</code> to be printed
  295. * @see java.lang.Double#toString(double)
  296. */
  297. public void print(double d) {
  298. write(String.valueOf(d));
  299. }
  300. /**
  301. * Print an array of characters. The characters are converted into bytes
  302. * according to the platform's default character encoding, and these bytes
  303. * are written in exactly the manner of the <code>{@link #write(int)}</code>
  304. * method.
  305. *
  306. * @param s The array of chars to be printed
  307. *
  308. * @throws NullPointerException If <code>s</code> is <code>null</code>
  309. */
  310. public void print(char s[]) {
  311. write(s);
  312. }
  313. /**
  314. * Print a string. If the argument is <code>null</code> then the string
  315. * <code>"null"</code> is printed. Otherwise, the string's characters are
  316. * converted into bytes according to the platform's default character
  317. * encoding, and these bytes are written in exactly the manner of the
  318. * <code>{@link #write(int)}</code> method.
  319. *
  320. * @param s The <code>String</code> to be printed
  321. */
  322. public void print(String s) {
  323. if (s == null) {
  324. s = "null";
  325. }
  326. write(s);
  327. }
  328. /**
  329. * Print an object. The string produced by the <code>{@link
  330. * java.lang.String#valueOf(Object)}</code> method is translated into bytes
  331. * according to the platform's default character encoding, and these bytes
  332. * are written in exactly the manner of the <code>{@link #write(int)}</code>
  333. * method.
  334. *
  335. * @param obj The <code>Object</code> to be printed
  336. * @see java.lang.Object#toString()
  337. */
  338. public void print(Object obj) {
  339. write(String.valueOf(obj));
  340. }
  341. /* Methods that do terminate lines */
  342. /**
  343. * Terminate the current line by writing the line separator string. The
  344. * line separator string is defined by the system property
  345. * <code>line.separator</code>, and is not necessarily a single newline
  346. * character (<code>'\n'</code>).
  347. */
  348. public void println() {
  349. newLine();
  350. }
  351. /**
  352. * Print a boolean value and then terminate the line. This method behaves
  353. * as though it invokes <code>{@link #print(boolean)}</code> and then
  354. * <code>{@link #println()}</code>.
  355. *
  356. * @param x the <code>boolean</code> value to be printed
  357. */
  358. public void println(boolean x) {
  359. synchronized (lock) {
  360. print(x);
  361. println();
  362. }
  363. }
  364. /**
  365. * Print a character and then terminate the line. This method behaves as
  366. * though it invokes <code>{@link #print(char)}</code> and then <code>{@link
  367. * #println()}</code>.
  368. *
  369. * @param x the <code>char</code> value to be printed
  370. */
  371. public void println(char x) {
  372. synchronized (lock) {
  373. print(x);
  374. println();
  375. }
  376. }
  377. /**
  378. * Print an integer and then terminate the line. This method behaves as
  379. * though it invokes <code>{@link #print(int)}</code> and then <code>{@link
  380. * #println()}</code>.
  381. *
  382. * @param x the <code>int</code> value to be printed
  383. */
  384. public void println(int x) {
  385. synchronized (lock) {
  386. print(x);
  387. println();
  388. }
  389. }
  390. /**
  391. * Print a long integer and then terminate the line. This method behaves
  392. * as though it invokes <code>{@link #print(long)}</code> and then
  393. * <code>{@link #println()}</code>.
  394. *
  395. * @param x the <code>long</code> value to be printed
  396. */
  397. public void println(long x) {
  398. synchronized (lock) {
  399. print(x);
  400. println();
  401. }
  402. }
  403. /**
  404. * Print a floating-point number and then terminate the line. This method
  405. * behaves as though it invokes <code>{@link #print(float)}</code> and then
  406. * <code>{@link #println()}</code>.
  407. *
  408. * @param x the <code>float</code> value to be printed
  409. */
  410. public void println(float x) {
  411. synchronized (lock) {
  412. print(x);
  413. println();
  414. }
  415. }
  416. /**
  417. * Print a double-precision floating-point number and then terminate the
  418. * line. This method behaves as though it invokes <code>{@link
  419. * #print(double)}</code> and then <code>{@link #println()}</code>.
  420. *
  421. * @param x the <code>double</code> value to be printed
  422. */
  423. public void println(double x) {
  424. synchronized (lock) {
  425. print(x);
  426. println();
  427. }
  428. }
  429. /**
  430. * Print an array of characters and then terminate the line. This method
  431. * behaves as though it invokes <code>{@link #print(char[])}</code> and then
  432. * <code>{@link #println()}</code>.
  433. *
  434. * @param x the array of <code>char</code> values to be printed
  435. */
  436. public void println(char x[]) {
  437. synchronized (lock) {
  438. print(x);
  439. println();
  440. }
  441. }
  442. /**
  443. * Print a String and then terminate the line. This method behaves as
  444. * though it invokes <code>{@link #print(String)}</code> and then
  445. * <code>{@link #println()}</code>.
  446. *
  447. * @param x the <code>String</code> value to be printed
  448. */
  449. public void println(String x) {
  450. synchronized (lock) {
  451. print(x);
  452. println();
  453. }
  454. }
  455. /**
  456. * Print an Object and then terminate the line. This method behaves as
  457. * though it invokes <code>{@link #print(Object)}</code> and then
  458. * <code>{@link #println()}</code>.
  459. *
  460. * @param x the <code>Object</code> value to be printed
  461. */
  462. public void println(Object x) {
  463. synchronized (lock) {
  464. print(x);
  465. println();
  466. }
  467. }
  468. }