Merge from the pain train
[official-gcc.git] / libjava / java / io / StringWriter.java
blob7b6e14193cd1349e3850d128c31ac9ae492b7aeb
1 /* StringWriter.java -- Writes bytes to a StringBuffer
2 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005 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 * @exception IOException If an error orrurs.
66 public void close () throws IOException
68 // JCL says this does nothing. This seems to violate the Writer
69 // contract, in that other methods should still throw an
70 // IOException after a close. Still, we just follow JCL.
73 /**
74 * This method flushes any buffered characters to the underlying output.
75 * It does nothing in this class.
77 public void flush ()
81 /**
82 * This method returns the <code>StringBuffer</code> object that this
83 * object is writing to. Note that this is the actual internal buffer, so
84 * any operations performed on it will affect this stream object.
86 * @return The <code>StringBuffer</code> object being written to
88 public StringBuffer getBuffer ()
90 return buffer;
93 /**
94 * This method initializes a new <code>StringWriter</code> to write to a
95 * <code>StringBuffer</code> initially sized to a default size of 16
96 * chars.
98 public StringWriter ()
100 this (DEFAULT_BUFFER_SIZE);
104 * This method initializes a new <code>StringWriter</code> to write to a
105 * <code>StringBuffer</code> with the specified initial size.
107 * @param size The initial size to make the <code>StringBuffer</code>
109 public StringWriter (int size)
111 super ();
112 buffer = new StringBuffer (size);
113 lock = buffer;
117 * This method returns the contents of the internal <code>StringBuffer</code>
118 * as a <code>String</code>.
120 * @return A <code>String</code> representing the chars written to
121 * this stream.
123 public String toString ()
125 return buffer.toString();
129 * This method writes a single character to the output, storing it in
130 * the internal buffer.
132 * @param oneChar The <code>char</code> to write, passed as an int.
134 public void write (int oneChar)
136 buffer.append((char) (oneChar & 0xFFFF));
140 * This method writes <code>len</code> chars from the specified
141 * array starting at index <code>offset</code> in that array to this
142 * stream by appending the chars to the end of the internal buffer.
144 * @param chars The array of chars to write
145 * @param offset The index into the array to start writing from
146 * @param len The number of chars to write
148 public void write (char[] chars, int offset, int len)
150 buffer.append(chars, offset, len);
154 * This method writes the characters in the specified <code>String</code>
155 * to the stream by appending them to the end of the internal buffer.
157 * @param str The <code>String</code> to write to the stream.
159 public void write (String str)
161 buffer.append(str);
165 * This method writes out <code>len</code> characters of the specified
166 * <code>String</code> to the stream starting at character position
167 * <code>offset</code> into the stream. This is done by appending the
168 * characters to the internal buffer.
170 * @param str The <code>String</code> to write characters from
171 * @param offset The character position to start writing from
172 * @param len The number of characters to write.
174 public void write (String str, int offset, int len)
176 // char[] tmpbuf = new char[len];
177 // str.getChars(offset, offset+len, tmpbuf, 0);
178 // buf.append(tmpbuf, 0, tmpbuf.length);
179 // This implementation assumes that String.substring is more
180 // efficient than using String.getChars and copying the data
181 // twice. For libgcj, this is true. For Classpath, it is not.
182 // FIXME.
183 buffer.append(str.substring(offset, offset + len));
187 * This is the <code>StringBuffer</code> that we use to store bytes that
188 * are written.
190 private StringBuffer buffer;