001/* StringWriter.java -- Writes bytes to a StringBuffer 002 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005 Free Software Foundation, Inc. 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038 039package java.io; 040 041// Wow is this a dumb class. CharArrayWriter can do all this and 042// more. I would redirect all calls to one in fact, but the javadocs say 043// use a StringBuffer so I will comply. 044 045/** 046 * This class writes chars to an internal <code>StringBuffer</code> that 047 * can then be used to retrieve a <code>String</code>. 048 * 049 * @author Aaron M. Renn (arenn@urbanophile.com) 050 * @author Tom Tromey (tromey@cygnus.com) 051 */ 052public class StringWriter extends Writer 053{ 054 /** 055 * This is the default size of the buffer if the user doesn't specify it. 056 * @specnote The JCL Volume 1 says that 16 is the default size. 057 */ 058 private static final int DEFAULT_BUFFER_SIZE = 16; 059 060 /** 061 * This method closes the stream. The contents of the internal buffer 062 * can still be retrieved, but future writes are not guaranteed to work. 063 * 064 * @exception IOException If an error orrurs. 065 */ 066 public void close () throws IOException 067 { 068 // JCL says this does nothing. This seems to violate the Writer 069 // contract, in that other methods should still throw an 070 // IOException after a close. Still, we just follow JCL. 071 } 072 073 /** 074 * This method flushes any buffered characters to the underlying output. 075 * It does nothing in this class. 076 */ 077 public void flush () 078 { 079 } 080 081 /** 082 * This method returns the <code>StringBuffer</code> object that this 083 * object is writing to. Note that this is the actual internal buffer, so 084 * any operations performed on it will affect this stream object. 085 * 086 * @return The <code>StringBuffer</code> object being written to 087 */ 088 public StringBuffer getBuffer () 089 { 090 return buffer; 091 } 092 093 /** 094 * This method initializes a new <code>StringWriter</code> to write to a 095 * <code>StringBuffer</code> initially sized to a default size of 16 096 * chars. 097 */ 098 public StringWriter () 099 { 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 /** @since 1.5 */ 187 public StringWriter append(char c) 188 { 189 write(c); 190 return this; 191 } 192 193 /** @since 1.5 */ 194 public StringWriter append(CharSequence cs) 195 { 196 write(cs == null ? "null" : cs.toString()); 197 return this; 198 } 199 200 /** @since 1.5 */ 201 public StringWriter append(CharSequence cs, int start, int end) 202 { 203 write(cs == null ? "null" : cs.subSequence(start, end).toString()); 204 return this; 205 } 206 207 /** 208 * This is the <code>StringBuffer</code> that we use to store bytes that 209 * are written. 210 */ 211 private StringBuffer buffer; 212}