home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Writer.java < prev    next >
Text File  |  1997-05-20  |  5KB  |  164 lines

  1. /*
  2.  * @(#)Writer.java    1.8 97/01/27
  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.  * Abstract class for writing to character streams.  The only methods that a
  28.  * subclass must implement are write(char[], int, int), flush(), and close().
  29.  * Most subclasses, however, will override some of the methods defined here in
  30.  * order to provide higher efficiency, additional functionality, or both.
  31.  *
  32.  * @see Writer
  33.  * @see   BufferedWriter
  34.  * @see   CharArrayWriter
  35.  * @see   FilterWriter
  36.  * @see   OutputStreamWriter
  37.  * @see     FileWriter
  38.  * @see   PipedWriter
  39.  * @see   PrintWriter
  40.  * @see   StringWriter
  41.  * @see Reader
  42.  *
  43.  * @version     1.8, 97/01/27
  44.  * @author    Mark Reinhold
  45.  * @since    JDK1.1
  46.  */
  47.  
  48. public abstract class Writer {
  49.  
  50.     /**
  51.      * The object used to synchronize operations on this stream.  For
  52.      * efficiency, a character-stream object may use an object other than
  53.      * itself to protect critical sections.  A subclass should therefore use
  54.      * the object in this field rather than <tt>this</tt> or a synchronized
  55.      * method.
  56.      */
  57.     protected Object lock;
  58.  
  59.     /**
  60.      * Create a new character-stream writer whose critical sections will
  61.      * synchronize on the writer itself.
  62.      */
  63.     protected Writer() {
  64.     this.lock = this;
  65.     }
  66.  
  67.     /**
  68.      * Create a new character-stream writer whose critical sections will
  69.      * synchronize on the given object.
  70.      */
  71.     protected Writer(Object lock) {
  72.     this.lock = lock;
  73.     }
  74.  
  75.     /**
  76.      * Write a single character.  The character to be written is contained in
  77.      * the 16 low-order bits of the given integer value; the 16 high-order bits
  78.      * are ignored.
  79.      *
  80.      * <p> Subclasses that intend to support efficient single-character output
  81.      * should override this method.
  82.      *
  83.      * @exception  IOException  If an I/O error occurs
  84.      */
  85.     public void write(int c) throws IOException {
  86.     synchronized (lock) {
  87.         char cb[] = new char[1];
  88.         cb[0] = (char) c;
  89.         write(cb, 0, 1);
  90.     }
  91.     }
  92.  
  93.     /**
  94.      * Write an array of characters.
  95.      *
  96.      * @param  cbuf  Array of characters to be written
  97.      * 
  98.      * @exception  IOException  If an I/O error occurs
  99.      */
  100.     public void write(char cbuf[]) throws IOException {
  101.     write(cbuf, 0, cbuf.length);
  102.     }
  103.  
  104.     /**
  105.      * Write a portion of an array of characters.
  106.      *
  107.      * @param  cbuf  Array of characters
  108.      * @param  off   Offset from which to start writing characters
  109.      * @param  len   Number of characters to write
  110.      *
  111.      * @exception  IOException  If an I/O error occurs
  112.      */
  113.     abstract public void write(char cbuf[], int off, int len) throws IOException;
  114.  
  115.     /**
  116.      * Write a string.
  117.      *
  118.      * @param  str  String to be written
  119.      *
  120.      * @exception  IOException  If an I/O error occurs
  121.      */
  122.     public void write(String str) throws IOException {
  123.     write(str, 0, str.length());
  124.     }
  125.  
  126.     /**
  127.      * Write a portion of a string.
  128.      *
  129.      * @param  str  A String
  130.      * @param  off  Offset from which to start writing characters
  131.      * @param  len  Number of characters to write
  132.      *
  133.      * @exception  IOException  If an I/O error occurs
  134.      */
  135.     public void write(String str, int off, int len) throws IOException {
  136.     synchronized (lock) {
  137.         char cbuf[] = new char[len];
  138.         str.getChars(off, len, cbuf, 0);
  139.         write(cbuf, 0, len);
  140.     }
  141.     }
  142.  
  143.     /**
  144.      * Flush the stream.  If the stream has saved any characters from the
  145.      * various write() methods in a buffer, write them immediately to their
  146.      * intended destination.  Then, if that destination is another character or
  147.      * byte stream, flush it.  Thus one flush() invocation will flush all the
  148.      * buffers in a chain of Writers and OutputStreams.
  149.      *
  150.      * @exception  IOException  If an I/O error occurs
  151.      */
  152.     abstract public void flush() throws IOException;
  153.  
  154.     /**
  155.      * Close the stream, flushing it first.  Once a stream has been closed,
  156.      * further write() or flush() invocations will cause an IOException to be
  157.      * thrown.  Closing a previously-closed stream, however, has no effect.
  158.      *
  159.      * @exception  IOException  If an I/O error occurs
  160.      */
  161.     abstract public void close() throws IOException;
  162.  
  163. }
  164.