2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / io / StringWriter.java
blob660ef166a2e59ecafaa8f5cfc950ecae7b18d855
1 /* StringWriter.java -- Writes bytes to a StringBuffer
2 Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.io;
41 // Wow is this a dumb class. CharArrayWriter can do all this and
42 // more. I would redirect all calls to one in fact, but the javadocs say
43 // use a StringBuffer so I will comply.
45 /**
46 * This class writes chars to an internal <code>StringBuffer</code> that
47 * can then be used to retrieve a <code>String</code>.
49 * @author Aaron M. Renn (arenn@urbanophile.com)
50 * @author Tom Tromey <tromey@cygnus.com>
52 public class StringWriter extends Writer
54 /**
55 * This is the default size of the buffer if the user doesn't specify it.
56 * @specnote The JCL Volume 1 says that 16 is the default size.
58 private static final int DEFAULT_BUFFER_SIZE = 16;
60 /**
61 * This method closes the stream. The contents of the internal buffer
62 * can still be retrieved, but future writes are not guaranteed to work.
64 public void close () throws IOException
66 // JCL says this does nothing. This seems to violate the Writer
67 // contract, in that other methods should still throw an
68 // IOException after a close. Still, we just follow JCL.
71 /**
72 * This method flushes any buffered characters to the underlying output.
73 * It does nothing in this class.
75 public void flush ()
79 /**
80 * This method returns the <code>StringBuffer</code> object that this
81 * object is writing to. Note that this is the actual internal buffer, so
82 * any operations performed on it will affect this stream object.
84 * @return The <code>StringBuffer</code> object being written to
86 public StringBuffer getBuffer ()
88 return buffer;
91 /**
92 * This method initializes a new <code>StringWriter</code> to write to a
93 * <code>StringBuffer</code> initially sized to a default size of 16
94 * chars.
96 public StringWriter ()
98 this (DEFAULT_BUFFER_SIZE);
102 * This method initializes a new <code>StringWriter</code> to write to a
103 * <code>StringBuffer</code> with the specified initial size.
105 * @param size The initial size to make the <code>StringBuffer</code>
107 public StringWriter (int size)
109 super ();
110 buffer = new StringBuffer (size);
111 lock = buffer;
115 * This method returns the contents of the internal <code>StringBuffer</code>
116 * as a <code>String</code>.
118 * @return A <code>String</code> representing the chars written to
119 * this stream.
121 public String toString ()
123 return buffer.toString();
127 * This method writes a single character to the output, storing it in
128 * the internal buffer.
130 * @param oneChar The <code>char</code> to write, passed as an int.
132 public void write (int oneChar)
134 buffer.append((char) (oneChar & 0xFFFF));
138 * This method writes <code>len</code> chars from the specified
139 * array starting at index <code>offset</code> in that array to this
140 * stream by appending the chars to the end of the internal buffer.
142 * @param chars The array of chars to write
143 * @param offset The index into the array to start writing from
144 * @param len The number of chars to write
146 public void write (char[] chars, int offset, int len)
148 buffer.append(chars, offset, len);
152 * This method writes the characters in the specified <code>String</code>
153 * to the stream by appending them to the end of the internal buffer.
155 * @param str The <code>String</code> to write to the stream.
157 public void write (String str)
159 buffer.append(str);
163 * This method writes out <code>len</code> characters of the specified
164 * <code>String</code> to the stream starting at character position
165 * <code>offset</code> into the stream. This is done by appending the
166 * characters to the internal buffer.
168 * @param str The <code>String</code> to write characters from
169 * @param offset The character position to start writing from
170 * @param len The number of characters to write.
172 public void write (String str, int offset, int len)
174 // char[] tmpbuf = new char[len];
175 // str.getChars(offset, offset+len, tmpbuf, 0);
176 // buf.append(tmpbuf, 0, tmpbuf.length);
177 // This implementation assumes that String.substring is more
178 // efficient than using String.getChars and copying the data
179 // twice. For libgcj, this is true. For Classpath, it is not.
180 // FIXME.
181 buffer.append(str.substring(offset, offset + len));
185 * This is the <code>StringBuffer</code> that we use to store bytes that
186 * are written.
188 private StringBuffer buffer;