home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / PrintStream.java < prev    next >
Text File  |  1998-09-22  |  14KB  |  520 lines

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