| 1 |
14 |
jlechner |
/* StringWriter.java -- Writes bytes to a StringBuffer
|
| 2 |
|
|
Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005 Free Software Foundation, Inc.
|
| 3 |
|
|
|
| 4 |
|
|
This file is part of GNU Classpath.
|
| 5 |
|
|
|
| 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.
|
| 10 |
|
|
|
| 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.
|
| 15 |
|
|
|
| 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., 51 Franklin Street, Fifth Floor, Boston, MA
|
| 19 |
|
|
02110-1301 USA.
|
| 20 |
|
|
|
| 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.
|
| 25 |
|
|
|
| 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. */
|
| 37 |
|
|
|
| 38 |
|
|
|
| 39 |
|
|
package java.io;
|
| 40 |
|
|
|
| 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.
|
| 44 |
|
|
|
| 45 |
|
|
/**
|
| 46 |
|
|
* This class writes chars to an internal <code>StringBuffer</code> that
|
| 47 |
|
|
* can then be used to retrieve a <code>String</code>.
|
| 48 |
|
|
*
|
| 49 |
|
|
* @author Aaron M. Renn (arenn@urbanophile.com)
|
| 50 |
|
|
* @author Tom Tromey (tromey@cygnus.com)
|
| 51 |
|
|
*/
|
| 52 |
|
|
public class StringWriter extends Writer
|
| 53 |
|
|
{
|
| 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.
|
| 57 |
|
|
*/
|
| 58 |
|
|
private static final int DEFAULT_BUFFER_SIZE = 16;
|
| 59 |
|
|
|
| 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.
|
| 63 |
|
|
*
|
| 64 |
|
|
* @exception IOException If an error orrurs.
|
| 65 |
|
|
*/
|
| 66 |
|
|
public void close () throws IOException
|
| 67 |
|
|
{
|
| 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.
|
| 71 |
|
|
}
|
| 72 |
|
|
|
| 73 |
|
|
/**
|
| 74 |
|
|
* This method flushes any buffered characters to the underlying output.
|
| 75 |
|
|
* It does nothing in this class.
|
| 76 |
|
|
*/
|
| 77 |
|
|
public void flush ()
|
| 78 |
|
|
{
|
| 79 |
|
|
}
|
| 80 |
|
|
|
| 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.
|
| 85 |
|
|
*
|
| 86 |
|
|
* @return The <code>StringBuffer</code> object being written to
|
| 87 |
|
|
*/
|
| 88 |
|
|
public StringBuffer getBuffer ()
|
| 89 |
|
|
{
|
| 90 |
|
|
return buffer;
|
| 91 |
|
|
}
|
| 92 |
|
|
|
| 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.
|
| 97 |
|
|
*/
|
| 98 |
|
|
public StringWriter ()
|
| 99 |
|
|
{
|
| 100 |
|
|
this (DEFAULT_BUFFER_SIZE);
|
| 101 |
|
|
}
|
| 102 |
|
|
|
| 103 |
|
|
/**
|
| 104 |
|
|
* This method initializes a new <code>StringWriter</code> to write to a
|
| 105 |
|
|
* <code>StringBuffer</code> with the specified initial size.
|
| 106 |
|
|
*
|
| 107 |
|
|
* @param size The initial size to make the <code>StringBuffer</code>
|
| 108 |
|
|
*/
|
| 109 |
|
|
public StringWriter (int size)
|
| 110 |
|
|
{
|
| 111 |
|
|
super ();
|
| 112 |
|
|
buffer = new StringBuffer (size);
|
| 113 |
|
|
lock = buffer;
|
| 114 |
|
|
}
|
| 115 |
|
|
|
| 116 |
|
|
/**
|
| 117 |
|
|
* This method returns the contents of the internal <code>StringBuffer</code>
|
| 118 |
|
|
* as a <code>String</code>.
|
| 119 |
|
|
*
|
| 120 |
|
|
* @return A <code>String</code> representing the chars written to
|
| 121 |
|
|
* this stream.
|
| 122 |
|
|
*/
|
| 123 |
|
|
public String toString ()
|
| 124 |
|
|
{
|
| 125 |
|
|
return buffer.toString();
|
| 126 |
|
|
}
|
| 127 |
|
|
|
| 128 |
|
|
/**
|
| 129 |
|
|
* This method writes a single character to the output, storing it in
|
| 130 |
|
|
* the internal buffer.
|
| 131 |
|
|
*
|
| 132 |
|
|
* @param oneChar The <code>char</code> to write, passed as an int.
|
| 133 |
|
|
*/
|
| 134 |
|
|
public void write (int oneChar)
|
| 135 |
|
|
{
|
| 136 |
|
|
buffer.append((char) (oneChar & 0xFFFF));
|
| 137 |
|
|
}
|
| 138 |
|
|
|
| 139 |
|
|
/**
|
| 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.
|
| 143 |
|
|
*
|
| 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
|
| 147 |
|
|
*/
|
| 148 |
|
|
public void write (char[] chars, int offset, int len)
|
| 149 |
|
|
{
|
| 150 |
|
|
buffer.append(chars, offset, len);
|
| 151 |
|
|
}
|
| 152 |
|
|
|
| 153 |
|
|
/**
|
| 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.
|
| 156 |
|
|
*
|
| 157 |
|
|
* @param str The <code>String</code> to write to the stream.
|
| 158 |
|
|
*/
|
| 159 |
|
|
public void write (String str)
|
| 160 |
|
|
{
|
| 161 |
|
|
buffer.append(str);
|
| 162 |
|
|
}
|
| 163 |
|
|
|
| 164 |
|
|
/**
|
| 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.
|
| 169 |
|
|
*
|
| 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.
|
| 173 |
|
|
*/
|
| 174 |
|
|
public void write (String str, int offset, int len)
|
| 175 |
|
|
{
|
| 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));
|
| 184 |
|
|
}
|
| 185 |
|
|
|
| 186 |
|
|
/**
|
| 187 |
|
|
* This is the <code>StringBuffer</code> that we use to store bytes that
|
| 188 |
|
|
* are written.
|
| 189 |
|
|
*/
|
| 190 |
|
|
private StringBuffer buffer;
|
| 191 |
|
|
}
|