home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / CharArrayWriter.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  174 lines

  1. /*
  2.  * @(#)CharArrayWriter.java    1.7 98/07/01
  3.  *
  4.  * Copyright 1995-1998 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.7, 07/01/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.      * @since   JDK1.1
  48.      */
  49.     public CharArrayWriter(int initialSize) {
  50.     buf = new char[initialSize];
  51.     }
  52.  
  53.     /**
  54.      * Writes a character to the buffer.
  55.      * @since   JDK1.1
  56.      */
  57.     public void write(int c) {
  58.     synchronized (lock) {
  59.         int newcount = count + 1;
  60.         if (newcount > buf.length) {
  61.         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  62.         System.arraycopy(buf, 0, newbuf, 0, count);
  63.         buf = newbuf;
  64.         }
  65.         buf[count] = (char)c;
  66.         count = newcount;
  67.     }
  68.     }
  69.  
  70.     /**
  71.      * Writes characters to the buffer.
  72.      * @param c    the data to be written
  73.      * @param off    the start offset in the data
  74.      * @param len    the number of chars that are written
  75.      * @since   JDK1.1
  76.      */
  77.     public void write(char c[], int off, int len) {
  78.     synchronized (lock) {
  79.         int newcount = count + len;
  80.         if (newcount > buf.length) {
  81.         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  82.         System.arraycopy(buf, 0, newbuf, 0, count);
  83.         buf = newbuf;
  84.         }
  85.         System.arraycopy(c, off, buf, count, len);
  86.         count = newcount;
  87.     }
  88.     }
  89.  
  90.     /**
  91.      * Write a portion of a string to the buffer.
  92.      * @param  str  String to be written from
  93.      * @param  off  Offset from which to start reading characters
  94.      * @param  len  Number of characters to be written
  95.      * @since   JDK1.1
  96.      */
  97.     public void write(String str, int off, int len) {
  98.     synchronized (lock) {
  99.         int newcount = count + len;
  100.         if (newcount > buf.length) {
  101.         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  102.         System.arraycopy(buf, 0, newbuf, 0, count);
  103.         buf = newbuf;
  104.         }
  105.         str.getChars(off, off + len, buf, count);
  106.         count = newcount;
  107.     }
  108.     }
  109.  
  110.     /**
  111.      * Writes the contents of the buffer to another character stream.
  112.      * @param out    the output stream to write to
  113.      * @since   JDK1.1
  114.      */
  115.     public void writeTo(Writer out) throws IOException {
  116.     synchronized (lock) {
  117.         out.write(buf, 0, count);
  118.     }
  119.     }
  120.  
  121.     /**
  122.      * Resets the buffer so that you can use it again without
  123.      * throwing away the already allocated buffer.
  124.      * @since   JDK1.1
  125.      */
  126.     public void reset() {
  127.     count = 0;
  128.     }
  129.  
  130.     /**
  131.      * Returns a copy of the input data.
  132.      * @since   JDK1.1
  133.      */
  134.     public char toCharArray()[] {
  135.     synchronized (lock) {
  136.         char newbuf[] = new char[count];
  137.         System.arraycopy(buf, 0, newbuf, 0, count);
  138.         return newbuf;
  139.     }
  140.     }
  141.  
  142.     /**
  143.      * Returns the current size of the buffer.
  144.      * @since   JDK1.1
  145.      */
  146.     public int size() {
  147.     return count;
  148.     }
  149.  
  150.     /**
  151.      * Converts input data to a string.
  152.      * @return the string.
  153.      * @since   JDK1.1
  154.      */
  155.     public String toString() {
  156.     synchronized (lock) {
  157.         return new String(toCharArray());
  158.     }
  159.     }
  160.  
  161.     /**
  162.      * Flush the stream.
  163.      * @since   JDK1.1
  164.      */
  165.     public void flush() { }
  166.  
  167.     /**
  168.      * Close the stream.  This method does not release the buffer, since its
  169.      * contents might still be required.
  170.      * @since   JDK1.1
  171.      */
  172.     public void close() { }
  173. }
  174.