home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / PrintWriter.java < prev    next >
Text File  |  1997-10-01  |  10KB  |  387 lines

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