home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / io / CharArrayWriter.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  3.9 KB  |  163 lines

  1. /*
  2.  * @(#)CharArrayWriter.java    1.8 98/03/18
  3.  *
  4.  * Copyright 1996, 1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * This class implements a character buffer that can be used as an Writer.
  19.  * The buffer automatically grows when data is written to the stream.  The data
  20.  * can be retrieved using toCharArray() and toString().
  21.  *
  22.  * @author    Herb Jellinek
  23.  * @version     1.8, 03/18/98
  24.  * @since       JDK1.1
  25.  */
  26. public
  27. class CharArrayWriter extends Writer {
  28.     /** 
  29.      * The buffer where data is stored.
  30.      */
  31.     protected char buf[];
  32.  
  33.     /**
  34.      * The number of chars in the buffer.
  35.      */
  36.     protected int count;
  37.  
  38.     /**
  39.      * Creates a new CharArrayWriter.
  40.      */
  41.     public CharArrayWriter() {
  42.     this(32);
  43.     }
  44.  
  45.     /**
  46.      * Creates a new CharArrayWriter with the specified initial size.
  47.      */
  48.     public CharArrayWriter(int initialSize) {
  49.     buf = new char[initialSize];
  50.     }
  51.  
  52.     /**
  53.      * Writes a character to the buffer.
  54.      */
  55.     public void write(int c) {
  56.     synchronized (lock) {
  57.         int newcount = count + 1;
  58.         if (newcount > buf.length) {
  59.         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  60.         System.arraycopy(buf, 0, newbuf, 0, count);
  61.         buf = newbuf;
  62.         }
  63.         buf[count] = (char)c;
  64.         count = newcount;
  65.     }
  66.     }
  67.  
  68.     /**
  69.      * Writes characters to the buffer.
  70.      * @param c    the data to be written
  71.      * @param off    the start offset in the data
  72.      * @param len    the number of chars that are written
  73.      */
  74.     public void write(char c[], int off, int len) {
  75.     synchronized (lock) {
  76.         int newcount = count + len;
  77.         if (newcount > buf.length) {
  78.         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  79.         System.arraycopy(buf, 0, newbuf, 0, count);
  80.         buf = newbuf;
  81.         }
  82.         System.arraycopy(c, off, buf, count, len);
  83.         count = newcount;
  84.     }
  85.     }
  86.  
  87.     /**
  88.      * Write a portion of a string to the buffer.
  89.      * @param  str  String to be written from
  90.      * @param  off  Offset from which to start reading characters
  91.      * @param  len  Number of characters to be written
  92.      */
  93.     public void write(String str, int off, int len) {
  94.     synchronized (lock) {
  95.         int newcount = count + len;
  96.         if (newcount > buf.length) {
  97.         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  98.         System.arraycopy(buf, 0, newbuf, 0, count);
  99.         buf = newbuf;
  100.         }
  101.         str.getChars(off, off + len, buf, count);
  102.         count = newcount;
  103.     }
  104.     }
  105.  
  106.     /**
  107.      * Writes the contents of the buffer to another character stream.
  108.      * @param out    the output stream to write to
  109.      */
  110.     public void writeTo(Writer out) throws IOException {
  111.     synchronized (lock) {
  112.         out.write(buf, 0, count);
  113.     }
  114.     }
  115.  
  116.     /**
  117.      * Resets the buffer so that you can use it again without
  118.      * throwing away the already allocated buffer.
  119.      */
  120.     public void reset() {
  121.     count = 0;
  122.     }
  123.  
  124.     /**
  125.      * Returns a copy of the input data.
  126.      */
  127.     public char toCharArray()[] {
  128.     synchronized (lock) {
  129.         char newbuf[] = new char[count];
  130.         System.arraycopy(buf, 0, newbuf, 0, count);
  131.         return newbuf;
  132.     }
  133.     }
  134.  
  135.     /**
  136.      * Returns the current size of the buffer.
  137.      */
  138.     public int size() {
  139.     return count;
  140.     }
  141.  
  142.     /**
  143.      * Converts input data to a string.
  144.      * @return the string.
  145.      */
  146.     public String toString() {
  147.     synchronized (lock) {
  148.         return new String(toCharArray());
  149.     }
  150.     }
  151.  
  152.     /**
  153.      * Flush the stream.
  154.      */
  155.     public void flush() { }
  156.  
  157.     /**
  158.      * Close the stream.  This method does not release the buffer, since its
  159.      * contents might still be required.
  160.      */
  161.     public void close() { }
  162. }
  163.