source: trunk/gcc/libjava/java/io/StringWriter.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 6.2 KB
Line 
1/* StringWriter.java -- Writes bytes to a StringBuffer
2 Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package 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 */
52public 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 public void close () throws IOException
65 {
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.
69 }
70
71 /**
72 * This method flushes any buffered characters to the underlying output.
73 * It does nothing in this class.
74 */
75 public void flush ()
76 {
77 }
78
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.
83 *
84 * @return The <code>StringBuffer</code> object being written to
85 */
86 public StringBuffer getBuffer ()
87 {
88 return buffer;
89 }
90
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.
95 */
96 public StringWriter ()
97 {
98 this (DEFAULT_BUFFER_SIZE);
99 }
100
101 /**
102 * This method initializes a new <code>StringWriter</code> to write to a
103 * <code>StringBuffer</code> with the specified initial size.
104 *
105 * @param size The initial size to make the <code>StringBuffer</code>
106 */
107 public StringWriter (int size)
108 {
109 super ();
110 buffer = new StringBuffer (size);
111 lock = buffer;
112 }
113
114 /**
115 * This method returns the contents of the internal <code>StringBuffer</code>
116 * as a <code>String</code>.
117 *
118 * @return A <code>String</code> representing the chars written to
119 * this stream.
120 */
121 public String toString ()
122 {
123 return buffer.toString();
124 }
125
126 /**
127 * This method writes a single character to the output, storing it in
128 * the internal buffer.
129 *
130 * @param oneChar The <code>char</code> to write, passed as an int.
131 */
132 public void write (int oneChar)
133 {
134 buffer.append((char) (oneChar & 0xFFFF));
135 }
136
137 /**
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.
141 *
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
145 */
146 public void write (char[] chars, int offset, int len)
147 {
148 buffer.append(chars, offset, len);
149 }
150
151 /**
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.
154 *
155 * @param str The <code>String</code> to write to the stream.
156 */
157 public void write (String str)
158 {
159 buffer.append(str);
160 }
161
162 /**
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.
167 *
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.
171 */
172 public void write (String str, int offset, int len)
173 {
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));
182 }
183
184 /**
185 * This is the <code>StringBuffer</code> that we use to store bytes that
186 * are written.
187 */
188 private StringBuffer buffer;
189}
Note: See TracBrowser for help on using the repository browser.