home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Internet / Javadraw / DATA.Z / PrintStream.java < prev    next >
Text File  |  1997-08-30  |  14KB  |  528 lines

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