1. /*
  2. * @(#)PrintStream.java 1.19 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. * A <code>PrintStream</code> adds functionality to another output stream,
  10. * namely the ability to print representations of various data values
  11. * conveniently. Two other features are provided as well. Unlike other output
  12. * streams, a <code>PrintStream</code> never throws an
  13. * <code>IOException</code> instead, exceptional situations merely set an
  14. * internal flag that can be tested via the <code>checkError</code> method.
  15. * Optionally, a <code>PrintStream</code> can be created so as to flush
  16. * automatically; this means that the <code>flush</code> method is
  17. * automatically invoked after a byte array is written, one of the
  18. * <code>println</code> methods is invoked, or a newline character or byte
  19. * (<code>'\n'</code>) is written.
  20. *
  21. * <p> All characters printed by a <code>PrintStream</code> are converted into
  22. * bytes using the platform's default character encoding. The <code{@link
  23. * PrintWriter}</code> class should be used in situations that require writing
  24. * characters rather than bytes.
  25. *
  26. * @version 1.19, 01/11/29
  27. * @author Frank Yellin
  28. * @author Mark Reinhold
  29. * @since JDK1.0
  30. */
  31. public class PrintStream extends FilterOutputStream {
  32. private boolean autoFlush = false;
  33. private boolean trouble = false;
  34. /**
  35. * Track both the text- and character-output streams, so that their buffers
  36. * can be flushed without flushing the entire stream.
  37. */
  38. private BufferedWriter textOut;
  39. private OutputStreamWriter charOut;
  40. /**
  41. * Create a new print stream. This stream will not flush automatically.
  42. *
  43. * @param out The output stream to which values and objects will be
  44. * printed
  45. *
  46. * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream)
  47. */
  48. public PrintStream(OutputStream out) {
  49. this(out, false);
  50. }
  51. /**
  52. * Create a new print stream.
  53. *
  54. * @param out The output stream to which values and objects will be
  55. * printed
  56. * @param autoFlush A boolean; if true, the output buffer will be flushed
  57. * whenever a byte array is written, one of the
  58. * <code>println</code> methods is invoked, or a newline
  59. * character or byte (<code>'\n'</code>) is written
  60. *
  61. * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
  62. */
  63. public PrintStream(OutputStream out, boolean autoFlush) {
  64. super(out);
  65. if (out == null) {
  66. throw new NullPointerException("Null output stream");
  67. }
  68. this.autoFlush = autoFlush;
  69. this.charOut = new OutputStreamWriter(this);
  70. this.textOut = new BufferedWriter(this.charOut);
  71. }
  72. /** Check to make sure that the stream has not been closed */
  73. private void ensureOpen() throws IOException {
  74. if (out == null)
  75. throw new IOException("Stream closed");
  76. }
  77. /**
  78. * Flush the stream. This is done by writing any buffered output bytes to
  79. * the underlying output stream and then flushing that stream.
  80. *
  81. * @see java.io.OutputStream#flush()
  82. */
  83. public void flush() {
  84. synchronized (this) {
  85. try {
  86. ensureOpen();
  87. out.flush();
  88. }
  89. catch (IOException x) {
  90. trouble = true;
  91. }
  92. }
  93. }
  94. private boolean closing = false; /* To avoid recursive closing */
  95. /**
  96. * Close the stream. This is done by flushing the stream and then closing
  97. * the underlying output stream.
  98. *
  99. * @see java.io.OutputStream#close()
  100. */
  101. public void close() {
  102. synchronized (this) {
  103. if (! closing) {
  104. closing = true;
  105. try {
  106. textOut.close();
  107. out.close();
  108. }
  109. catch (IOException x) {
  110. trouble = true;
  111. }
  112. textOut = null;
  113. charOut = null;
  114. out = null;
  115. }
  116. }
  117. }
  118. /**
  119. * Flush the stream and check its error state. The internal error state
  120. * is set to <code>true</code> when the underlying output stream throws an
  121. * <code>IOException</code> other than <code>InterruptedIOException</code>,
  122. * and when the <code>setError</code> method is invoked. If an operation
  123. * on the underlying output stream throws an
  124. * <code>InterruptedIOException</code>, then the <code>PrintStream</code>
  125. * converts the exception back into an interrupt by doing:
  126. * <pre>
  127. * Thread.currentThread().interrupt();
  128. * </pre>
  129. * or the equivalent.
  130. *
  131. * @return True if and only if this stream has encountered an
  132. * <code>IOException</code> other than
  133. * <code>InterruptedIOException</code>, or the
  134. * <code>setError</code> method has been invoked
  135. */
  136. public boolean checkError() {
  137. if (out != null)
  138. flush();
  139. return trouble;
  140. }
  141. /**
  142. * Set the error state of the stream to <code>true</code>.
  143. *
  144. * @since JDK1.1
  145. */
  146. protected void setError() {
  147. trouble = true;
  148. }
  149. /*
  150. * Exception-catching, synchronized output operations,
  151. * which also implement the write() methods of OutputStream
  152. */
  153. /**
  154. * Write the specified byte to this stream. If the byte is a newline and
  155. * automatic flushing is enabled then the <code>flush</code> method will be
  156. * invoked.
  157. *
  158. * <p> Note that the byte is written as given; to write a character that
  159. * will be translated according to the platform's default character
  160. * encoding, use the <code>print(char)</code> or <code>println(char)</code>
  161. * methods.
  162. *
  163. * @param b The byte to be written
  164. * @see #print(char)
  165. * @see #println(char)
  166. */
  167. public void write(int b) {
  168. try {
  169. synchronized (this) {
  170. ensureOpen();
  171. out.write(b);
  172. if ((b == '\n') && autoFlush)
  173. out.flush();
  174. }
  175. }
  176. catch (InterruptedIOException x) {
  177. Thread.currentThread().interrupt();
  178. }
  179. catch (IOException x) {
  180. trouble = true;
  181. }
  182. }
  183. /**
  184. * Write <code>len</code> bytes from the specified byte array starting at
  185. * offset <code>off</code> to this stream. If automatic flushing is
  186. * enabled then the <code>flush</code> method will be invoked.
  187. *
  188. * <p> Note that the bytes will be written as given; to write characters
  189. * that will be translated according to the platform's default character
  190. * encoding, use the <code>print(char)</code> or <code>println(char)</code>
  191. * methods.
  192. *
  193. * @param buf A byte array
  194. * @param off Offset from which to start taking bytes
  195. * @param len Number of bytes to write
  196. */
  197. public void write(byte buf[], int off, int len) {
  198. try {
  199. synchronized (this) {
  200. ensureOpen();
  201. out.write(buf, off, len);
  202. if (autoFlush)
  203. out.flush();
  204. }
  205. }
  206. catch (InterruptedIOException x) {
  207. Thread.currentThread().interrupt();
  208. }
  209. catch (IOException x) {
  210. trouble = true;
  211. }
  212. }
  213. /*
  214. * The following private methods on the text- and character-output streams
  215. * always flush the stream buffers, so that writes to the underlying byte
  216. * stream occur as promptly as with the original PrintStream.
  217. */
  218. private void write(char buf[]) {
  219. try {
  220. synchronized (this) {
  221. ensureOpen();
  222. textOut.write(buf);
  223. textOut.flushBuffer();
  224. charOut.flushBuffer();
  225. if (autoFlush) {
  226. for (int i = 0; i < buf.length; i++)
  227. if (buf[i] == '\n')
  228. out.flush();
  229. }
  230. }
  231. }
  232. catch (InterruptedIOException x) {
  233. Thread.currentThread().interrupt();
  234. }
  235. catch (IOException x) {
  236. trouble = true;
  237. }
  238. }
  239. private void write(String s) {
  240. try {
  241. synchronized (this) {
  242. ensureOpen();
  243. textOut.write(s);
  244. textOut.flushBuffer();
  245. charOut.flushBuffer();
  246. if (autoFlush && (s.indexOf('\n') >= 0))
  247. out.flush();
  248. }
  249. }
  250. catch (InterruptedIOException x) {
  251. Thread.currentThread().interrupt();
  252. }
  253. catch (IOException x) {
  254. trouble = true;
  255. }
  256. }
  257. private void newLine() {
  258. try {
  259. synchronized (this) {
  260. ensureOpen();
  261. textOut.newLine();
  262. textOut.flushBuffer();
  263. charOut.flushBuffer();
  264. if (autoFlush)
  265. out.flush();
  266. }
  267. }
  268. catch (InterruptedIOException x) {
  269. Thread.currentThread().interrupt();
  270. }
  271. catch (IOException x) {
  272. trouble = true;
  273. }
  274. }
  275. /* Methods that do not terminate lines */
  276. /**
  277. * Print a boolean value. The string produced by <code>{@link
  278. * java.lang.String#valueOf(boolean)} is translated into bytes according to
  279. * the platform's default character encoding, and these bytes are written
  280. * in exactly the manner of the <code>{@link #write(int)} method.
  281. *
  282. * @param b The <code>boolean</code> to be printed
  283. */
  284. public void print(boolean b) {
  285. write(b ? "true" : "false");
  286. }
  287. /**
  288. * Print a character. The character is translated into one or more bytes
  289. * according to the platform's default character encoding, and these bytes
  290. * are written in exactly the manner of the <code>{@link #write(int)}
  291. * method.
  292. *
  293. * @param c The <code>char</code> to be printed
  294. */
  295. public void print(char c) {
  296. write(String.valueOf(c));
  297. }
  298. /**
  299. * Print an integer. The string produced by <code>{@link
  300. * java.lang.String#valueOf(int)} is translated into bytes according to the
  301. * platform's default character encoding, and these bytes are written in
  302. * exactly the manner of the <code>{@link #write(int)} method.
  303. *
  304. * @param i The <code>int</code> to be printed
  305. * @see java.lang.Integer#toString(int)
  306. */
  307. public void print(int i) {
  308. write(String.valueOf(i));
  309. }
  310. /**
  311. * Print a long integer. The string produced by <code>{@link
  312. * java.lang.String#valueOf(long)} is translated into bytes according to
  313. * the platform's default character encoding, and these bytes are written
  314. * in exactly the manner of the <code>{@link #write(int)} method.
  315. *
  316. * @param l The <code>long</code> to be printed
  317. * @see java.lang.Long#toString(long)
  318. */
  319. public void print(long l) {
  320. write(String.valueOf(l));
  321. }
  322. /**
  323. * Print a floating-point number. The string produced by <code>{@link
  324. * java.lang.String#valueOf(float)} is translated into bytes according to
  325. * the platform's default character encoding, and these bytes are written
  326. * in exactly the manner of the <code>{@link #write(int)} method.
  327. *
  328. * @param f The <code>float</code> to be printed
  329. * @see java.lang.Float#toString(float)
  330. */
  331. public void print(float f) {
  332. write(String.valueOf(f));
  333. }
  334. /**
  335. * Print a double-precision floating-point number. The string produced by
  336. * <code>{@link java.lang.String#valueOf(double)} is translated into bytes
  337. * according to the platform's default character encoding, and these bytes
  338. * are written in exactly the manner of the <code>{@link #write(int)}
  339. * method.
  340. *
  341. * @param d The <code>double</code> to be printed
  342. * @see java.lang.Double#toString(double)
  343. */
  344. public void print(double d) {
  345. write(String.valueOf(d));
  346. }
  347. /**
  348. * Print an array of characters. The characters are converted into bytes
  349. * according to the platform's default character encoding, and these bytes
  350. * are written in exactly the manner of the <code>{@link #write(int)}
  351. * method.
  352. *
  353. * @param s The array of chars to be printed
  354. *
  355. * @throws NullPointerException If <code>s</code> is <code>null</code>
  356. */
  357. public void print(char s[]) {
  358. write(s);
  359. }
  360. /**
  361. * Print a string. If the argument is <code>null</code> then the string
  362. * <code>"null"</code> is printed. Otherwise, the string's characters are
  363. * converted into bytes according to the platform's default character
  364. * encoding, and these bytes are written in exactly the manner of the
  365. * <code>{@link #write(int)} method.
  366. *
  367. * @param s The <code>String</code> to be printed
  368. */
  369. public void print(String s) {
  370. if (s == null) {
  371. s = "null";
  372. }
  373. write(s);
  374. }
  375. /**
  376. * Print an object. The string produced by the <code>{@link
  377. * java.lang.String#valueOf(Object)} method is translated into bytes
  378. * according to the platform's default character encoding, and these bytes
  379. * are written in exactly the manner of the <code>{@link #write(int)}
  380. * method.
  381. *
  382. * @param obj The <code>Object</code> to be printed
  383. * @see java.lang.Object#toString()
  384. */
  385. public void print(Object obj) {
  386. write(String.valueOf(obj));
  387. }
  388. /* Methods that do terminate lines */
  389. /**
  390. * Terminate the current line by writing the line separator string. The
  391. * line separator string is defined by the system property
  392. * <code>line.separator</code>, and is not necessarily a single newline
  393. * character (<code>'\n'</code>).
  394. */
  395. public void println() {
  396. newLine();
  397. }
  398. /**
  399. * Print a boolean and then terminate the line. This method behaves as
  400. * though it invokes <code>{@link #print(boolean)} and then <code>{@link
  401. * #println()}</code>.
  402. */
  403. public void println(boolean x) {
  404. synchronized (this) {
  405. print(x);
  406. newLine();
  407. }
  408. }
  409. /**
  410. * Print a character and then terminate the line. This method behaves as
  411. * though it invokes <code>{@link #print(char)} and then <code>{@link
  412. * #println()}</code>.
  413. */
  414. public void println(char x) {
  415. synchronized (this) {
  416. print(x);
  417. newLine();
  418. }
  419. }
  420. /**
  421. * Print an integer and then terminate the line. This method behaves as
  422. * though it invokes <code>{@link #print(int)} and then <code>{@link
  423. * #println()}</code>.
  424. */
  425. public void println(int x) {
  426. synchronized (this) {
  427. print(x);
  428. newLine();
  429. }
  430. }
  431. /**
  432. * Print a long and then terminate the line. This method behaves as
  433. * though it invokes <code>{@link #print(long)} and then <code>{@link
  434. * #println()}</code>.
  435. */
  436. public void println(long x) {
  437. synchronized (this) {
  438. print(x);
  439. newLine();
  440. }
  441. }
  442. /**
  443. * Print a float and then terminate the line. This method behaves as
  444. * though it invokes <code>{@link #print(float)} and then <code>{@link
  445. * #println()}</code>.
  446. */
  447. public void println(float x) {
  448. synchronized (this) {
  449. print(x);
  450. newLine();
  451. }
  452. }
  453. /**
  454. * Print a double and then terminate the line. This method behaves as
  455. * though it invokes <code>{@link #print(double)} and then <code>{@link
  456. * #println()}</code>.
  457. */
  458. public void println(double x) {
  459. synchronized (this) {
  460. print(x);
  461. newLine();
  462. }
  463. }
  464. /**
  465. * Print an array of characters and then terminate the line. This method
  466. * behaves as though it invokes <code>{@link #print(char[])} and then
  467. * <code>{@link #println()}</code>.
  468. */
  469. public void println(char x[]) {
  470. synchronized (this) {
  471. print(x);
  472. newLine();
  473. }
  474. }
  475. /**
  476. * Print a String and then terminate the line. This method behaves as
  477. * though it invokes <code>{@link #print(String)} and then <code>{@link
  478. * #println()}</code>.
  479. */
  480. public void println(String x) {
  481. synchronized (this) {
  482. print(x);
  483. newLine();
  484. }
  485. }
  486. /**
  487. * Print an Object and then terminate the line. This method behaves as
  488. * though it invokes <code>{@link #print(Object)} and then <code>{@link
  489. * #println()}</code>.
  490. */
  491. public void println(Object x) {
  492. synchronized (this) {
  493. print(x);
  494. newLine();
  495. }
  496. }
  497. }