home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / java / io / PrintStream.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  7.9 KB  |  338 lines

  1. /*
  2.  * @(#)PrintStream.java    1.24 95/12/19 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.io;
  21.  
  22. import java.io.DataOutputStream;
  23.  
  24. /**
  25.  * This class implements an output stream that has
  26.  * additional methods for printing. You can specify
  27.  * that the stream should be flushed every time a
  28.  * newline character is written.<p>
  29.  *
  30.  * <em>The top byte of 16 bit characters is discarded.</em><p>
  31.  * Example:
  32.  * <pre>
  33.  *    System.out.println("Hello world!");
  34.  *    System.out.print("x = ");
  35.  *    System.out.println(x);
  36.  *    System.out.println("y = " + y);
  37.  * </pre>
  38.  *
  39.  * @version     1.24, 12/19/95
  40.  * @author    Arthur van Hoff
  41.  */
  42. public
  43. class PrintStream extends FilterOutputStream {
  44.     private boolean autoflush;
  45.     private boolean trouble;
  46.  
  47.     /**
  48.      * Creates a new PrintStream.
  49.      * @param out    the output stream
  50.      */
  51.     public PrintStream(OutputStream out) {
  52.     this(out, false);
  53.     trouble = false;
  54.     }
  55.  
  56.     /**
  57.      * Creates a new PrintStream, with auto flushing.
  58.      * @param out    the output stream
  59.      * @param autoflush if true the stream automatically flushes
  60.      *        its output when a newline character is printed
  61.      */
  62.     public PrintStream(OutputStream out, boolean autoflush) {
  63.     super(out);
  64.     this.autoflush = autoflush;
  65.     trouble = false;
  66.     }
  67.  
  68.     /**
  69.      * Writes a byte. This method will block until the byte is actually
  70.      * written.
  71.      * @param b the byte
  72.      * @exception IOException If an I/O error has occurred.
  73.      */
  74.     public void write(int b) {
  75.         try {
  76.         out.write(b);
  77.         if (autoflush && (b == '\n')) {
  78.             out.flush();
  79.         }
  80.       } catch (InterruptedIOException ex) {
  81.         // We've been interrupted.  Make sure we're still interrupted.
  82.         Thread.currentThread().interrupt();
  83.     } catch (IOException ex) {
  84.         trouble = true;
  85.     }
  86.     }
  87.  
  88.     /**
  89.      * Writes a sub array of bytes. 
  90.      * @param b    the data to be written
  91.      * @param off    the start offset in the data
  92.      * @param len    the number of bytes that are written
  93.      * @exception IOException If an I/O error has occurred.
  94.      */
  95.     public void write(byte b[], int off, int len) {
  96.     try {
  97.         out.write(b, off, len);
  98.         if (autoflush) {
  99.             out.flush();
  100.         }
  101.       } catch (InterruptedIOException ex) {
  102.         // We've been interrupted.  Make sure we're still interrupted.
  103.         Thread.currentThread().interrupt();
  104.     } catch (IOException ex) {
  105.         trouble = true;
  106.     }
  107.     }
  108.  
  109.     /**
  110.      * Flushes the stream. This will write any buffered
  111.      * output bytes.
  112.      */
  113.     public void flush() {
  114.     try {
  115.         super.flush();
  116.     } catch (IOException ex) {
  117.         trouble = true;
  118.     }
  119.     }
  120.  
  121.     /**
  122.      * Closes the stream.
  123.      */
  124.     public void close() {
  125.     try {
  126.         super.close();
  127.     } catch (IOException ex) {
  128.         trouble = true;
  129.     }
  130.     }
  131.  
  132.     /**
  133.      * Flushes the print stream and returns whether or not there was
  134.      * an error on the output stream.  Errors are cumulative; once the
  135.      * print stream encounters an error this routine will continue to
  136.      * return true on all successive calls.
  137.      * @return true if the print stream has ever encountered an error
  138.      * on the output stream.
  139.      */
  140.     public boolean checkError() {
  141.     flush();
  142.     return trouble;
  143.     }
  144.  
  145.     /**
  146.      * Prints an object.
  147.      * @param obj the object to be printed
  148.      */
  149.     public void print(Object obj) {
  150.     print(String.valueOf(obj));
  151.     }
  152.  
  153.     /**
  154.      * Prints a String.
  155.      * @param s the String to be printed
  156.      */
  157.  
  158.     private native boolean isOutputStreamLocalised(DataOutputStream dos);
  159.  
  160.     synchronized public void print(String s) {
  161.     if (s == null) {
  162.         s = "null";
  163.     }
  164.  
  165.     if ((out instanceof DataOutputStream) &&
  166.         (isOutputStreamLocalised((DataOutputStream)out)))
  167.     {
  168.         byte B[] = com.ms.lang.SystemX.JavaStringToLocalString(s.toCharArray());
  169.         write(B, 0, B.length);
  170.     }
  171.     else
  172.     {
  173.         int len = s.length();
  174.         for (int i = 0 ; i < len ; i++) {
  175.             write(s.charAt(i));
  176.         }
  177.     }
  178.     }
  179.  
  180.     /**
  181.      * Prints an array of characters.
  182.      * @param s the array of chars to be printed
  183.      */
  184.  
  185.     synchronized public void print(char s[]) {
  186.  
  187.         if ((out instanceof DataOutputStream) &&
  188.             (isOutputStreamLocalised((DataOutputStream)out)))
  189.         {
  190.             byte B[] = com.ms.lang.SystemX.JavaStringToLocalString(s);
  191.             write(B, 0, B.length);
  192.         }
  193.         else
  194.         {
  195.             for (int i = 0 ; i < s.length ; i++) {
  196.                 write(s[i]);
  197.             }
  198.         }
  199.  
  200.     }
  201.  
  202.     /**
  203.      * Prints an character.
  204.      * @param c the character to be printed
  205.      */
  206.     public void print(char c) {
  207.     print(String.valueOf(c));
  208.     }
  209.  
  210.     /**
  211.      * Prints an integer.
  212.      * @param i the integer to be printed
  213.      */
  214.     public void print(int i) {
  215.     print(String.valueOf(i));
  216.     }
  217.  
  218.     /**
  219.      * Prints a long.
  220.      * @param l the long to be printed.
  221.      */
  222.     public void print(long l) {
  223.     print(String.valueOf(l));
  224.     }
  225.  
  226.     /**
  227.      * Prints a float.
  228.      * @param f the float to be printed
  229.      */
  230.     public void print(float f) {
  231.     print(String.valueOf(f));
  232.     }
  233.  
  234.     /**
  235.      * Prints a double.
  236.      * @param d the double to be printed
  237.      */
  238.     public void print(double d) {
  239.     print(String.valueOf(d));
  240.     }
  241.  
  242.     /**
  243.      * Prints a boolean.
  244.      * @param b the boolean to be printed
  245.      */
  246.     public void print(boolean b) {
  247.     print(b ? "true" : "false");
  248.     }
  249.     
  250.     /**
  251.      * Prints a newline.
  252.      */
  253.     public void println() {
  254.     write('\n');
  255.     }
  256.     
  257.     /**
  258.      * Prints an object followed by a newline.
  259.      * @param obj the object to be printed
  260.      */
  261.     synchronized public void println(Object obj) {
  262.     print(obj);
  263.     write('\n');
  264.     }
  265.  
  266.     /**
  267.      * Prints a string followed by a newline.
  268.      * @param s the String to be printed
  269.      */
  270.     synchronized public void println(String s) {
  271.     print(s);
  272.     write('\n');
  273.     }
  274.     
  275.     /**
  276.      * Prints an array of characters followed by a newline.
  277.      * @param s the array of characters to be printed
  278.      */
  279.     synchronized public void println(char s[]) {
  280.     print(s);
  281.     write('\n');
  282.     }
  283.     
  284.     /**
  285.      * Prints a character followed by a newline.
  286.      * @param c the character to be printed
  287.      */
  288.     synchronized public void println(char c) {
  289.     print(c);
  290.     write('\n');
  291.     }
  292.  
  293.     /**
  294.      * Prints an integer followed by a newline.
  295.      * @param i the integer to be printed
  296.      */
  297.     synchronized public void println(int i) {
  298.     print(i);
  299.     write('\n');
  300.     }
  301.  
  302.     /**
  303.      * Prints a long followed by a newline.
  304.      * @param l the long to be printed
  305.      */
  306.     synchronized public void println(long l) {
  307.     print(l);
  308.     write('\n');
  309.     }
  310.  
  311.     /**
  312.      * Prints a float followed by a newline.
  313.      * @param f the float to be printed
  314.      */
  315.     synchronized public void println(float f) {
  316.     print(f);
  317.     write('\n');
  318.     }
  319.  
  320.     /**
  321.      * Prints a double followed by a newline.
  322.      * @param d the double to be printed
  323.      */
  324.     synchronized public void println(double d) {
  325.     print(d);
  326.     write('\n');
  327.     }
  328.  
  329.     /**
  330.      * Prints a boolean followed by a newline.
  331.      * @param b the boolean to be printed
  332.      */
  333.     synchronized public void println(boolean b) {
  334.     print(b);
  335.     write('\n');
  336.     }
  337. }
  338.