2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 * A specialized {@link Writer} that writes characters to a {@code StringBuffer}
22 * in a sequential manner, appending them in the process. The result can later
23 * be queried using the {@link #StringWriter(int)} or {@link #toString()}
28 public class StringWriter extends Writer {
30 private StringBuffer buf;
33 * Constructs a new {@code StringWriter} which has a {@link StringBuffer}
34 * allocated with the default size of 16 characters. The {@code
35 * StringBuffer} is also the {@code lock} used to synchronize access to this
38 public StringWriter() {
40 buf = new StringBuffer(16);
45 * Constructs a new {@code StringWriter} which has a {@link StringBuffer}
46 * allocated with a size of {@code initialSize} characters. The {@code
47 * StringBuffer} is also the {@code lock} used to synchronize access to this
51 * the intial size of the target string buffer.
53 public StringWriter(int initialSize) {
54 if (initialSize < 0) {
55 throw new IllegalArgumentException();
57 buf = new StringBuffer(initialSize);
62 * Calling this method has no effect. In contrast to most {@code Writer} subclasses,
63 * the other methods in {@code StringWriter} do not throw an {@code IOException} if
64 * {@code close()} has been called.
67 * if an error occurs while closing this writer.
70 public void close() throws IOException {
75 * Calling this method has no effect.
83 * Gets a reference to this writer's internal {@link StringBuffer}. Any
84 * changes made to the returned buffer are reflected in this writer.
86 * @return a reference to this writer's internal {@code StringBuffer}.
88 public StringBuffer getBuffer() {
93 * Gets a copy of the contents of this writer as a string.
95 * @return this writer's contents as a string.
98 public String toString() {
99 return buf.toString();
103 * Writes {@code count} characters starting at {@code offset} in {@code buf}
104 * to this writer's {@code StringBuffer}.
107 * the non-null character array to write.
109 * the index of the first character in {@code chars} to write.
111 * the maximum number of characters to write.
112 * @throws IndexOutOfBoundsException
113 * if {@code offset < 0} or {@code count < 0}, or if {@code
114 * offset + count} is greater than the size of {@code buf}.
117 public void write(char[] chars, int offset, int count) {
118 // avoid int overflow
119 // BEGIN android-changed
120 // Exception priorities (in case of multiple errors) differ from
121 // RI, but are spec-compliant.
122 // removed redundant check, added null check, used (offset | count) < 0
123 // instead of (offset < 0) || (count < 0) to safe one operation
125 throw new NullPointerException("chars == null");
127 if ((offset | count) < 0 || count > chars.length - offset) {
128 throw new IndexOutOfBoundsException();
130 // END android-changed
134 buf.append(chars, offset, count);
138 * Writes one character to this writer's {@code StringBuffer}. Only the two
139 * least significant bytes of the integer {@code oneChar} are written.
142 * the character to write to this writer's {@code StringBuffer}.
145 public void write(int oneChar) {
146 buf.append((char) oneChar);
150 * Writes the characters from the specified string to this writer's {@code
154 * the non-null string containing the characters to write.
157 public void write(String str) {
162 * Writes {@code count} characters from {@code str} starting at {@code
163 * offset} to this writer's {@code StringBuffer}.
166 * the non-null string containing the characters to write.
168 * the index of the first character in {@code str} to write.
170 * the number of characters from {@code str} to write.
171 * @throws StringIndexOutOfBoundsException
172 * if {@code offset < 0} or {@code count < 0}, or if {@code
173 * offset + count} is greater than the length of {@code str}.
176 public void write(String str, int offset, int count) {
177 String sub = str.substring(offset, offset + count);
182 * Appends the character {@code c} to this writer's {@code StringBuffer}.
183 * This method works the same way as {@link #write(int)}.
186 * the character to append to the target stream.
187 * @return this writer.
190 public StringWriter append(char c) {
196 * Appends the character sequence {@code csq} to this writer's {@code
197 * StringBuffer}. This method works the same way as {@code
198 * StringWriter.write(csq.toString())}. If {@code csq} is {@code null}, then
199 * the string "null" is written to the target stream.
202 * the character sequence appended to the target.
203 * @return this writer.
206 public StringWriter append(CharSequence csq) {
210 write(csq.toString());
216 * Appends a subsequence of the character sequence {@code csq} to this
217 * writer's {@code StringBuffer}. This method works the same way as {@code
218 * StringWriter.writer(csq.subsequence(start, end).toString())}. If {@code
219 * csq} is {@code null}, then the specified subsequence of the string "null"
220 * will be written to the target.
223 * the character sequence appended to the target.
225 * the index of the first char in the character sequence appended
228 * the index of the character following the last character of the
229 * subsequence appended to the target.
230 * @return this writer.
231 * @throws IndexOutOfBoundsException
232 * if {@code start > end}, {@code start < 0}, {@code end < 0} or
233 * either {@code start} or {@code end} are greater or equal than
234 * the length of {@code csq}.
237 public StringWriter append(CharSequence csq, int start, int end) {
241 String output = csq.subSequence(start, end).toString();
242 write(output, 0, output.length());