StringWriter.java 6.22 KB
Newer Older
1
/* StringWriter.java -- Writes bytes to a StringBuffer
2
   Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
Tom Tromey committed
3

4
This file is part of GNU Classpath.
Tom Tromey committed
5

6 7 8 9 10 11 12 13 14
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
 
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
Tom Tromey committed
15

16 17 18 19
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Tom Tromey committed
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */
Tom Tromey committed
37 38


39
package java.io;
Tom Tromey committed
40

41 42 43 44 45 46 47 48 49 50 51
// Wow is this a dumb class.  CharArrayWriter can do all this and
// more.  I would redirect all calls to one in fact, but the javadocs say
// use a StringBuffer so I will comply.

/**
  * This class writes chars to an internal <code>StringBuffer</code> that
  * can then be used to retrieve a <code>String</code>.
  *
  * @author Aaron M. Renn (arenn@urbanophile.com)
  * @author Tom Tromey <tromey@cygnus.com>
  */
Tom Tromey committed
52 53
public class StringWriter extends Writer
{
54 55 56 57 58 59 60 61 62 63
  /**
   * This is the default size of the buffer if the user doesn't specify it.
   * @specnote The JCL Volume 1 says that 16 is the default size.
   */
  private static final int DEFAULT_BUFFER_SIZE = 16;

  /**
   * This method closes the stream.  The contents of the internal buffer
   * can still be retrieved, but future writes are not guaranteed to work.
   */
64
  public void close () throws IOException
Tom Tromey committed
65 66
  {
    // JCL says this does nothing.  This seems to violate the Writer
67
    // contract, in that other methods should still throw an
Tom Tromey committed
68 69 70
    // IOException after a close.  Still, we just follow JCL.
  }

71 72 73 74
  /**
   * This method flushes any buffered characters to the underlying output.
   * It does nothing in this class.
   */
Tom Tromey committed
75 76 77 78
  public void flush ()
  {
  }

79 80 81 82 83 84 85
  /**
   * This method returns the <code>StringBuffer</code> object that this
   * object is writing to.  Note that this is the actual internal buffer, so
   * any operations performed on it will affect this stream object.
   *
   * @return The <code>StringBuffer</code> object being written to
   */
Tom Tromey committed
86 87 88 89 90
  public StringBuffer getBuffer ()
  {
    return buffer;
  }

91 92 93 94 95
  /**
   * This method initializes a new <code>StringWriter</code> to write to a
   * <code>StringBuffer</code> initially sized to a default size of 16
   * chars.
   */
Tom Tromey committed
96 97
  public StringWriter ()
  {
98
    this (DEFAULT_BUFFER_SIZE);
Tom Tromey committed
99 100
  }

101 102 103 104 105 106
  /**
   * This method initializes a new <code>StringWriter</code> to write to a
   * <code>StringBuffer</code> with the specified initial size.
   *
   * @param size The initial size to make the <code>StringBuffer</code>
   */
107
  public StringWriter (int size)
Tom Tromey committed
108 109 110 111 112 113
  {
    super ();
    buffer = new StringBuffer (size);
    lock = buffer;
  }

114 115 116 117 118 119 120
  /**
   * This method returns the contents of the internal <code>StringBuffer</code>
   * as a <code>String</code>.
   *
   * @return A <code>String</code> representing the chars written to
   * this stream. 
   */
Tom Tromey committed
121 122 123 124 125
  public String toString ()
  {
    return buffer.toString();
  }

126 127 128 129 130 131
  /**
   * This method writes a single character to the output, storing it in
   * the internal buffer.
   *
   * @param oneChar The <code>char</code> to write, passed as an int.
   */
Tom Tromey committed
132 133
  public void write (int oneChar)
  {
134
    buffer.append((char) (oneChar & 0xFFFF));
Tom Tromey committed
135 136
  }

137 138 139 140 141 142 143 144 145
  /**
   * This method writes <code>len</code> chars from the specified
   * array starting at index <code>offset</code> in that array to this
   * stream by appending the chars to the end of the internal buffer.
   *
   * @param chars The array of chars to write
   * @param offset The index into the array to start writing from
   * @param len The number of chars to write
   */
Tom Tromey committed
146 147 148 149 150
  public void write (char[] chars, int offset, int len)
  {
    buffer.append(chars, offset, len);
  }

151 152 153 154 155 156
  /**
   * This method writes the characters in the specified <code>String</code>
   * to the stream by appending them to the end of the internal buffer.
   *
   * @param str The <code>String</code> to write to the stream.
   */
Tom Tromey committed
157 158 159 160 161
  public void write (String str)
  {
    buffer.append(str);
  }

162 163 164 165 166 167 168 169 170 171
  /**
   * This method writes out <code>len</code> characters of the specified
   * <code>String</code> to the stream starting at character position
   * <code>offset</code> into the stream.  This is done by appending the
   * characters to the internal buffer.
   *
   * @param str The <code>String</code> to write characters from
   * @param offset The character position to start writing from
   * @param len The number of characters to write.
   */ 
Tom Tromey committed
172 173
  public void write (String str, int offset, int len)
  {
174 175 176 177 178 179 180
//      char[] tmpbuf = new char[len];
//      str.getChars(offset, offset+len, tmpbuf, 0);
//      buf.append(tmpbuf, 0, tmpbuf.length);
    // This implementation assumes that String.substring is more
    // efficient than using String.getChars and copying the data
    // twice.  For libgcj, this is true.  For Classpath, it is not.
    // FIXME.
Tom Tromey committed
181 182 183
    buffer.append(str.substring(offset, offset + len));
  }

184 185 186 187
  /**
   * This is the <code>StringBuffer</code> that we use to store bytes that
   * are written.
   */
Tom Tromey committed
188 189
  private StringBuffer buffer;
}