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

  1. /*
  2.  * @(#)CharArrayWriter.java    1.6 97/01/22
  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.  * This class implements a character buffer that can be used as an Writer.
  27.  * The buffer automatically grows when data is written to the stream.  The data
  28.  * can be retrieved using toCharArray() and toString().
  29.  *
  30.  * @author    Herb Jellinek
  31.  * @version     1.6, 01/22/97
  32.  * @since       JDK1.1
  33.  */
  34. public
  35. class CharArrayWriter extends Writer {
  36.     /** 
  37.      * The buffer where data is stored.
  38.      */
  39.     protected char buf[];
  40.  
  41.     /**
  42.      * The number of chars in the buffer.
  43.      */
  44.     protected int count;
  45.  
  46.     /**
  47.      * Creates a new CharArrayWriter.
  48.      */
  49.     public CharArrayWriter() {
  50.     this(32);
  51.     }
  52.  
  53.     /**
  54.      * Creates a new CharArrayWriter with the specified initial size.
  55.      * @since   JDK1.1
  56.      */
  57.     public CharArrayWriter(int initialSize) {
  58.     buf = new char[initialSize];
  59.     }
  60.  
  61.     /**
  62.      * Writes a character to the buffer.
  63.      * @since   JDK1.1
  64.      */
  65.     public void write(int c) {
  66.     synchronized (lock) {
  67.         int newcount = count + 1;
  68.         if (newcount > buf.length) {
  69.         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  70.         System.arraycopy(buf, 0, newbuf, 0, count);
  71.         buf = newbuf;
  72.         }
  73.         buf[count] = (char)c;
  74.         count = newcount;
  75.     }
  76.     }
  77.  
  78.     /**
  79.      * Writes characters to the buffer.
  80.      * @param c    the data to be written
  81.      * @param off    the start offset in the data
  82.      * @param len    the number of chars that are written
  83.      * @since   JDK1.1
  84.      */
  85.     public void write(char c[], int off, int len) {
  86.     synchronized (lock) {
  87.         int newcount = count + len;
  88.         if (newcount > buf.length) {
  89.         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  90.         System.arraycopy(buf, 0, newbuf, 0, count);
  91.         buf = newbuf;
  92.         }
  93.         System.arraycopy(c, off, buf, count, len);
  94.         count = newcount;
  95.     }
  96.     }
  97.  
  98.     /**
  99.      * Write a portion of a string to the buffer.
  100.      * @param  str  String to be written from
  101.      * @param  off  Offset from which to start reading characters
  102.      * @param  len  Number of characters to be written
  103.      * @since   JDK1.1
  104.      */
  105.     public void write(String str, int off, int len) {
  106.     synchronized (lock) {
  107.         int newcount = count + len;
  108.         if (newcount > buf.length) {
  109.         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  110.         System.arraycopy(buf, 0, newbuf, 0, count);
  111.         buf = newbuf;
  112.         }
  113.         str.getChars(off, off + len, buf, count);
  114.         count = newcount;
  115.     }
  116.     }
  117.  
  118.     /**
  119.      * Writes the contents of the buffer to another character stream.
  120.      * @param out    the output stream to write to
  121.      * @since   JDK1.1
  122.      */
  123.     public void writeTo(Writer out) throws IOException {
  124.     synchronized (lock) {
  125.         out.write(buf, 0, count);
  126.     }
  127.     }
  128.  
  129.     /**
  130.      * Resets the buffer so that you can use it again without
  131.      * throwing away the already allocated buffer.
  132.      * @since   JDK1.1
  133.      */
  134.     public void reset() {
  135.     count = 0;
  136.     }
  137.  
  138.     /**
  139.      * Returns a copy of the input data.
  140.      * @since   JDK1.1
  141.      */
  142.     public char toCharArray()[] {
  143.     synchronized (lock) {
  144.         char newbuf[] = new char[count];
  145.         System.arraycopy(buf, 0, newbuf, 0, count);
  146.         return newbuf;
  147.     }
  148.     }
  149.  
  150.     /**
  151.      * Returns the current size of the buffer.
  152.      * @since   JDK1.1
  153.      */
  154.     public int size() {
  155.     return count;
  156.     }
  157.  
  158.     /**
  159.      * Converts input data to a string.
  160.      * @return the string.
  161.      * @since   JDK1.1
  162.      */
  163.     public String toString() {
  164.     synchronized (lock) {
  165.         return new String(toCharArray());
  166.     }
  167.     }
  168.  
  169.     /**
  170.      * Flush the stream.
  171.      * @since   JDK1.1
  172.      */
  173.     public void flush() { }
  174.  
  175.     /**
  176.      * Close the stream.  This method does not release the buffer, since its
  177.      * contents might still be required.
  178.      * @since   JDK1.1
  179.      */
  180.     public void close() { }
  181. }
  182.