home *** CD-ROM | disk | FTP | other *** search
/ Chip: Shareware for Win 95 / Chip-Shareware-Win95.bin / ostatni / powerj / java.z / PrintStream.java < prev    next >
Encoding:
Java Source  |  1996-05-03  |  7.1 KB  |  312 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. /**
  23.  * This class implements an output stream that has
  24.  * additional methods for printing. You can specify
  25.  * that the stream should be flushed every time a
  26.  * newline character is written.<p>
  27.  *
  28.  * <em>The top byte of 16 bit characters is discarded.</em><p>
  29.  * Example:
  30.  * <pre>
  31.  *    System.out.println("Hello world!");
  32.  *    System.out.print("x = ");
  33.  *    System.out.println(x);
  34.  *    System.out.println("y = " + y);
  35.  * </pre>
  36.  *
  37.  * @version     1.24, 19 Dec 1995
  38.  * @author    Arthur van Hoff
  39.  */
  40. public
  41. class PrintStream extends FilterOutputStream {
  42.     private boolean autoflush;
  43.     private boolean trouble;
  44.  
  45.     /**
  46.      * Creates a new PrintStream.
  47.      * @param out    the output stream
  48.      */
  49.     public PrintStream(OutputStream out) {
  50.     this(out, false);
  51.     trouble = false;
  52.     }
  53.  
  54.     /**
  55.      * Creates a new PrintStream, with auto flushing.
  56.      * @param out    the output stream
  57.      * @param autoflush if true the stream automatically flushes
  58.      *        its output when a newline character is printed
  59.      */
  60.     public PrintStream(OutputStream out, boolean autoflush) {
  61.     super(out);
  62.     this.autoflush = autoflush;
  63.     trouble = false;
  64.     }
  65.  
  66.     /**
  67.      * Writes a byte. This method will block until the byte is actually
  68.      * written.
  69.      * @param b the byte
  70.      * @exception IOException If an I/O error has occurred.
  71.      */
  72.     public void write(int b) {
  73.         try {
  74.         out.write(b);
  75.         if (autoflush && (b == '\n')) {
  76.             out.flush();
  77.         }
  78.       } catch (InterruptedIOException ex) {
  79.         // We've been interrupted.  Make sure we're still interrupted.
  80.         Thread.currentThread().interrupt();
  81.     } catch (IOException ex) {
  82.         trouble = true;
  83.     }
  84.     }
  85.  
  86.     /**
  87.      * Writes a sub array of bytes. 
  88.      * @param b    the data to be written
  89.      * @param off    the start offset in the data
  90.      * @param len    the number of bytes that are written
  91.      * @exception IOException If an I/O error has occurred.
  92.      */
  93.     public void write(byte b[], int off, int len) {
  94.     try {
  95.         out.write(b, off, len);
  96.         if (autoflush) {
  97.             out.flush();
  98.         }
  99.       } catch (InterruptedIOException ex) {
  100.         // We've been interrupted.  Make sure we're still interrupted.
  101.         Thread.currentThread().interrupt();
  102.     } catch (IOException ex) {
  103.         trouble = true;
  104.     }
  105.     }
  106.  
  107.     /**
  108.      * Flushes the stream. This will write any buffered
  109.      * output bytes.
  110.      */
  111.     public void flush() {
  112.     try {
  113.         super.flush();
  114.     } catch (IOException ex) {
  115.         trouble = true;
  116.     }
  117.     }
  118.  
  119.     /**
  120.      * Closes the stream.
  121.      */
  122.     public void close() {
  123.     try {
  124.         super.close();
  125.     } catch (IOException ex) {
  126.         trouble = true;
  127.     }
  128.     }
  129.  
  130.     /**
  131.      * Flushes the print stream and returns whether or not there was
  132.      * an error on the output stream.  Errors are cumulative; once the
  133.      * print stream encounters an error this routine will continue to
  134.      * return true on all successive calls.
  135.      * @return true if the print stream has ever encountered an error
  136.      * on the output stream.
  137.      */
  138.     public boolean checkError() {
  139.     flush();
  140.     return trouble;
  141.     }
  142.  
  143.     /**
  144.      * Prints an object.
  145.      * @param obj the object to be printed
  146.      */
  147.     public void print(Object obj) {
  148.     print(String.valueOf(obj));
  149.     }
  150.  
  151.     /**
  152.      * Prints a String.
  153.      * @param s the String to be printed
  154.      */
  155.     synchronized public void print(String s) {
  156.     if (s == null) {
  157.         s = "null";
  158.     }
  159.  
  160.     int len = s.length();
  161.     for (int i = 0 ; i < len ; i++) {
  162.         write(s.charAt(i));
  163.     }
  164.     }
  165.  
  166.     /**
  167.      * Prints an array of characters.
  168.      * @param s the array of chars to be printed
  169.      */
  170.     synchronized public void print(char s[]) {
  171.     for (int i = 0 ; i < s.length ; i++) {
  172.         write(s[i]);
  173.     }
  174.     }
  175.  
  176.     /**
  177.      * Prints an character.
  178.      * @param c the character to be printed
  179.      */
  180.     public void print(char c) {
  181.     print(String.valueOf(c));
  182.     }
  183.  
  184.     /**
  185.      * Prints an integer.
  186.      * @param i the integer to be printed
  187.      */
  188.     public void print(int i) {
  189.     print(String.valueOf(i));
  190.     }
  191.  
  192.     /**
  193.      * Prints a long.
  194.      * @param l the long to be printed.
  195.      */
  196.     public void print(long l) {
  197.     print(String.valueOf(l));
  198.     }
  199.  
  200.     /**
  201.      * Prints a float.
  202.      * @param f the float to be printed
  203.      */
  204.     public void print(float f) {
  205.     print(String.valueOf(f));
  206.     }
  207.  
  208.     /**
  209.      * Prints a double.
  210.      * @param d the double to be printed
  211.      */
  212.     public void print(double d) {
  213.     print(String.valueOf(d));
  214.     }
  215.  
  216.     /**
  217.      * Prints a boolean.
  218.      * @param b the boolean to be printed
  219.      */
  220.     public void print(boolean b) {
  221.     print(b ? "true" : "false");
  222.     }
  223.     
  224.     /**
  225.      * Prints a newline.
  226.      */
  227.     public void println() {
  228.     write('\n');
  229.     }
  230.     
  231.     /**
  232.      * Prints an object followed by a newline.
  233.      * @param obj the object to be printed
  234.      */
  235.     synchronized public void println(Object obj) {
  236.     print(obj);
  237.     write('\n');
  238.     }
  239.  
  240.     /**
  241.      * Prints a string followed by a newline.
  242.      * @param s the String to be printed
  243.      */
  244.     synchronized public void println(String s) {
  245.     print(s);
  246.     write('\n');
  247.     }
  248.     
  249.     /**
  250.      * Prints an array of characters followed by a newline.
  251.      * @param s the array of characters to be printed
  252.      */
  253.     synchronized public void println(char s[]) {
  254.     print(s);
  255.     write('\n');
  256.     }
  257.     
  258.     /**
  259.      * Prints a character followed by a newline.
  260.      * @param c the character to be printed
  261.      */
  262.     synchronized public void println(char c) {
  263.     print(c);
  264.     write('\n');
  265.     }
  266.  
  267.     /**
  268.      * Prints an integer followed by a newline.
  269.      * @param i the integer to be printed
  270.      */
  271.     synchronized public void println(int i) {
  272.     print(i);
  273.     write('\n');
  274.     }
  275.  
  276.     /**
  277.      * Prints a long followed by a newline.
  278.      * @param l the long to be printed
  279.      */
  280.     synchronized public void println(long l) {
  281.     print(l);
  282.     write('\n');
  283.     }
  284.  
  285.     /**
  286.      * Prints a float followed by a newline.
  287.      * @param f the float to be printed
  288.      */
  289.     synchronized public void println(float f) {
  290.     print(f);
  291.     write('\n');
  292.     }
  293.  
  294.     /**
  295.      * Prints a double followed by a newline.
  296.      * @param d the double to be printed
  297.      */
  298.     synchronized public void println(double d) {
  299.     print(d);
  300.     write('\n');
  301.     }
  302.  
  303.     /**
  304.      * Prints a boolean followed by a newline.
  305.      * @param b the boolean to be printed
  306.      */
  307.     synchronized public void println(boolean b) {
  308.     print(b);
  309.     write('\n');
  310.     }
  311. }
  312.