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

  1. /*
  2.  * @(#)BufferedWriter.java    1.12 97/03/03
  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.  * Write text to a character-output stream, buffering characters so as to
  28.  * provide for the efficient writing of single characters, arrays, and strings.
  29.  *
  30.  * <p> The buffer size may be specified, or the default size may be accepted.
  31.  * The default is large enough for most purposes.
  32.  *
  33.  * <p> A newLine() method is provided, which uses the platform's own notion of
  34.  * line separator as defined by the system property <tt>line.separator</tt>.
  35.  * Not all platforms use the newline character ('\n') to terminate lines.
  36.  * Calling this method to terminate each output line is therefore preferred to
  37.  * writing a newline character directly.
  38.  *
  39.  * <p> In general, a Writer sends its output immediately to the underlying
  40.  * character or byte stream.  Unless prompt output is required, it is advisable
  41.  * to wrap a BufferedWriter around any Writer whose write() operations may be
  42.  * costly, such as FileWriters and OutputStreamWriters.  For example,
  43.  *
  44.  * <pre>
  45.  * PrintWriter out
  46.  *   = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
  47.  * </pre>
  48.  *
  49.  * will buffer the PrintWriter's output to the file.  Without buffering, each
  50.  * invocation of a print() method would cause characters to be converted into
  51.  * bytes that would then be written immediately to the file, which can be very
  52.  * inefficient.
  53.  *
  54.  * @see PrintWriter
  55.  * @see FileWriter
  56.  * @see OutputStreamWriter
  57.  *
  58.  * @version     1.12, 97/03/03
  59.  * @author    Mark Reinhold
  60.  * @since    JDK1.1
  61.  */
  62.  
  63. public class BufferedWriter extends Writer {
  64.  
  65.     private Writer out;
  66.  
  67.     private char cb[];
  68.     private int nChars, nextChar;
  69.  
  70.     private static int defaultCharBufferSize = 8192;
  71.  
  72.     /**
  73.      * Line separator string.  This is the value of the line.separator
  74.      * property at the moment that the stream was created.
  75.      */
  76.     private String lineSeparator;
  77.  
  78.     /**
  79.      * Create a buffered character-output stream that uses a default-sized
  80.      * output buffer.
  81.      *
  82.      * @param  out  A Writer
  83.      */
  84.     public BufferedWriter(Writer out) {
  85.     this(out, defaultCharBufferSize);
  86.     }
  87.  
  88.     /**
  89.      * Create a new buffered character-output stream that uses an output
  90.      * buffer of the given size.
  91.      *
  92.      * @param  out  A Writer
  93.      * @param  sz   Output-buffer size, a positive integer
  94.      *
  95.      * @exception  IllegalArgumentException  If sz is <= 0
  96.      */
  97.     public BufferedWriter(Writer out, int sz) {
  98.     super(out);
  99.     if (sz <= 0)
  100.         throw new IllegalArgumentException("Buffer size <= 0");
  101.     this.out = out;
  102.     cb = new char[sz];
  103.     nChars = sz;
  104.     nextChar = 0;
  105.     lineSeparator = System.getProperty("line.separator");
  106.     }
  107.  
  108.     /** Check to make sure that the stream has not been closed */
  109.     private void ensureOpen() throws IOException {
  110.     if (out == null)
  111.         throw new IOException("Stream closed");
  112.     }
  113.  
  114.     /**
  115.      * Flush the output buffer to the underlying character stream, without
  116.      * flushing the stream itself.  This method is non-private only so that it
  117.      * may be invoked by PrintStream.
  118.      */
  119.     void flushBuffer() throws IOException {
  120.     synchronized (lock) {
  121.         ensureOpen();
  122.         if (nextChar == 0)
  123.         return;
  124.         out.write(cb, 0, nextChar);
  125.         nextChar = 0;
  126.     }
  127.     }
  128.  
  129.     /**
  130.      * Write a single character.
  131.      *
  132.      * @exception  IOException  If an I/O error occurs
  133.      */
  134.     public void write(int c) throws IOException {
  135.     synchronized (lock) {
  136.         ensureOpen();
  137.         if (nextChar >= nChars)
  138.         flushBuffer();
  139.         cb[nextChar++] = (char) c;
  140.     }
  141.     }
  142.  
  143.     /**
  144.      * Write a portion of an array of characters.
  145.      *
  146.      * <p> Ordinarily this method stores characters from the given array into
  147.      * this stream's buffer, flushing the buffer to the underlying stream as
  148.      * needed.  If the requested length is at least as large as the buffer,
  149.      * however, then this method will flush the buffer and write the characters
  150.      * directly to the underlying stream.  Thus redundant
  151.      * <code>BufferedWriter</code>s will not copy data unnecessarily.
  152.      *
  153.      * @param  cbuf  A character array
  154.      * @param  off   Offset from which to start reading characters
  155.      * @param  len   Number of characters to write
  156.      *
  157.      * @exception  IOException  If an I/O error occurs
  158.      */
  159.     public void write(char cbuf[], int off, int len) throws IOException {
  160.     synchronized (lock) {
  161.         ensureOpen();
  162.  
  163.         if (len >= nChars) {
  164.         /* If the request length exceeds the size of the output buffer,
  165.            flush the buffer and then write the data directly.  In this
  166.            way buffered streams will cascade harmlessly. */
  167.         flushBuffer();
  168.         out.write(cbuf, off, len);
  169.         return;
  170.         }
  171.  
  172.         int b = off, t = off + len;
  173.         while (b < t) {
  174.         int d = Math.min(nChars - nextChar, t - b);
  175.         System.arraycopy(cbuf, b, cb, nextChar, d);
  176.         b += d;
  177.         nextChar += d;
  178.         if (nextChar >= nChars)
  179.             flushBuffer();
  180.         }
  181.     }
  182.     }
  183.  
  184.     /**
  185.      * Write a portion of a String.
  186.      *
  187.      * @param  s     String to be written
  188.      * @param  off   Offset from which to start reading characters
  189.      * @param  len   Number of characters to be written
  190.      *
  191.      * @exception  IOException  If an I/O error occurs
  192.      */
  193.     public void write(String s, int off, int len) throws IOException {
  194.     synchronized (lock) {
  195.         ensureOpen();
  196.  
  197.         int b = off, t = off + len;
  198.         while (b < t) {
  199.         int d = Math.min(nChars - nextChar, t - b);
  200.         s.getChars(b, b + d, cb, nextChar);
  201.         b += d;
  202.         nextChar += d;
  203.         if (nextChar >= nChars)
  204.             flushBuffer();
  205.         }
  206.     }
  207.     }
  208.  
  209.     /**
  210.      * Write a line separator.  The line separator string is defined by the
  211.      * system property <tt>line.separator</tt>, and is not necessarily a single
  212.      * newline ('\n') character.
  213.      *
  214.      * @exception  IOException  If an I/O error occurs
  215.      */
  216.     public void newLine() throws IOException {
  217.     write(lineSeparator);
  218.     }
  219.  
  220.     /**
  221.      * Flush the stream.
  222.      *
  223.      * @exception  IOException  If an I/O error occurs
  224.      */
  225.     public void flush() throws IOException {
  226.     synchronized (lock) {
  227.         flushBuffer();
  228.         out.flush();
  229.     }
  230.     }
  231.  
  232.     /**
  233.      * Close the stream.
  234.      *
  235.      * @exception  IOException  If an I/O error occurs
  236.      */
  237.     public void close() throws IOException {
  238.     synchronized (lock) {
  239.         if (out == null)
  240.         return;
  241.         flushBuffer();
  242.         out.close();
  243.         out = null;
  244.         cb = null;
  245.     }
  246.     }
  247.  
  248. }
  249.