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