home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / io / PrintWriter.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  8.8 KB  |  384 lines

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