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

  1. /*
  2.  * @(#)StringWriter.java    1.5 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.  * A character stream that collects its output in a string buffer, which can
  28.  * then be used to construct a string.
  29.  *
  30.  * @version     1.5, 97/01/27
  31.  * @author    Mark Reinhold
  32.  * @since    JDK1.1
  33.  */
  34.  
  35. public class StringWriter extends Writer {
  36.  
  37.     private StringBuffer buf;
  38.  
  39.     /**
  40.      * Create a new string writer, using the default initial string-buffer
  41.      * size.
  42.      */
  43.     public StringWriter() {
  44.     buf = new StringBuffer();
  45.     lock = buf;
  46.     }
  47.  
  48.     /**
  49.      * Create a new string writer, using the specified initial string-buffer
  50.      * size.
  51.      */
  52.     protected StringWriter(int initialSize) {
  53.     buf = new StringBuffer(initialSize);
  54.     lock = buf;
  55.     }
  56.  
  57.     /**
  58.      * Write a single character.
  59.      */
  60.     public void write(int c) {
  61.     buf.append((char) c);
  62.     }
  63.  
  64.     /**
  65.      * Write a portion of an array of characters.
  66.      *
  67.      * @param  cbuf  Array of characters
  68.      * @param  off   Offset from which to start writing characters
  69.      * @param  len   Number of characters to write
  70.      */
  71.     public void write(char cbuf[], int off, int len) {
  72.     buf.append(cbuf, off, len);
  73.     }
  74.  
  75.     /**
  76.      * Write a string.
  77.      */
  78.     public void write(String str) {
  79.     buf.append(str);
  80.     }
  81.  
  82.     /**
  83.      * Write a portion of a string.
  84.      *
  85.      * @param  str  String to be written
  86.      * @param  off  Offset from which to start writing characters
  87.      * @param  len  Number of characters to write
  88.      */
  89.     public void write(String str, int off, int len)  {
  90.     char cbuf[] = new char[len];
  91.     str.getChars(off, len, cbuf, 0);
  92.     buf.append(cbuf);
  93.     }
  94.  
  95.     /**
  96.      * Return the buffer's current value as a string.
  97.      */
  98.     public String toString() {
  99.     return buf.toString();
  100.     }
  101.  
  102.     /**
  103.      * Return the string buffer itself.
  104.      */
  105.     public StringBuffer getBuffer() {
  106.     return buf;
  107.     }
  108.  
  109.     /**
  110.      * Flush the stream.
  111.      */
  112.     public void flush() { }
  113.  
  114.     /**
  115.      * Close the stream.  This method does not release the buffer, since its
  116.      * contents might still be required.
  117.      */
  118.     public void close() { }
  119.  
  120. }
  121.