001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.io;
019:
020: /**
021: * StringWriter is an class for writing Character Streams to a StringBuffer. The
022: * characters written can then be returned as a String. This is used for
023: * capturing output sent to a Writer by substituting a StringWriter.
024: *
025: * @see StringReader
026: */
027: public class StringWriter extends Writer {
028:
029: private StringBuffer buf;
030:
031: /**
032: * Constructs a new StringWriter which has a StringBuffer allocated with the
033: * default size of 16 characters. The StringBuffer is also the
034: * <code>lock</code> used to synchronize access to this Writer.
035: */
036: public StringWriter() {
037: super ();
038: buf = new StringBuffer(16);
039: lock = buf;
040: }
041:
042: /**
043: * Constructs a new StringWriter which has a StringBuffer allocated with the
044: * size of <code>initialSize</code> characters. The StringBuffer is also
045: * the <code>lock</code> used to synchronize access to this Writer.
046: *
047: * @param initialSize
048: * the intial number of characters
049: */
050: public StringWriter(int initialSize) {
051: if (initialSize < 0) {
052: throw new IllegalArgumentException();
053: }
054: buf = new StringBuffer(initialSize);
055: lock = buf;
056: }
057:
058: /**
059: * Close this Writer. This is the concrete implementation required. This
060: * particular implementation does nothing.
061: *
062: * @throws IOException
063: * If an IO error occurs closing this StringWriter.
064: */
065: @Override
066: public void close() throws IOException {
067: /* empty */
068: }
069:
070: /**
071: * Flush this Writer. This is the concrete implementation required. This
072: * particular implementation does nothing.
073: */
074: @Override
075: public void flush() {
076: /* empty */
077: }
078:
079: /**
080: * Answer the contents of this StringWriter as a StringBuffer. Any changes
081: * made to the StringBuffer by the receiver or the caller are reflected in
082: * this StringWriter.
083: *
084: * @return this StringWriters local StringBuffer.
085: */
086: public StringBuffer getBuffer() {
087: return buf;
088: }
089:
090: /**
091: * Answer the contents of this StringWriter as a String. Any changes made to
092: * the StringBuffer by the receiver after returning will not be reflected in
093: * the String returned to the caller.
094: *
095: * @return this StringWriters current contents as a String.
096: */
097: @Override
098: public String toString() {
099: return buf.toString();
100: }
101:
102: /**
103: * Writes <code>count</code> characters starting at <code>offset</code>
104: * in <code>cbuf</code> to this StringWriter.
105: *
106: * @param cbuf
107: * the non-null array containing characters to write.
108: * @param offset
109: * offset in buf to retrieve characters
110: * @param count
111: * maximum number of characters to write
112: *
113: * @throws ArrayIndexOutOfBoundsException
114: * If offset or count are outside of bounds.
115: */
116: @Override
117: public void write(char[] cbuf, int offset, int count) {
118: // avoid int overflow
119: if (offset < 0 || offset > cbuf.length || count < 0
120: || count > cbuf.length - offset) {
121: throw new IndexOutOfBoundsException();
122: }
123: if (count == 0) {
124: return;
125: }
126: buf.append(cbuf, offset, count);
127: }
128:
129: /**
130: * Writes the specified character <code>oneChar</code> to this
131: * StringWriter. This implementation writes the low order two bytes to the
132: * Stream.
133: *
134: * @param oneChar
135: * The character to write
136: */
137: @Override
138: public void write(int oneChar) {
139: buf.append((char) oneChar);
140: }
141:
142: /**
143: * Writes the characters from the String <code>str</code> to this
144: * StringWriter.
145: *
146: * @param str
147: * the non-null String containing the characters to write.
148: */
149: @Override
150: public void write(String str) {
151: buf.append(str);
152: }
153:
154: /**
155: * Writes <code>count</code> number of characters starting at
156: * <code>offset</code> from the String <code>str</code> to this
157: * StringWriter.
158: *
159: * @param str
160: * the non-null String containing the characters to write.
161: * @param offset
162: * the starting point to retrieve characters.
163: * @param count
164: * the number of characters to retrieve and write.
165: *
166: * @throws ArrayIndexOutOfBoundsException
167: * If offset or count are outside of bounds.
168: */
169: @Override
170: public void write(String str, int offset, int count) {
171: String sub = str.substring(offset, offset + count);
172: buf.append(sub);
173: }
174:
175: /**
176: * Append a char <code>c</code>to the StringWriter. The
177: * StringWriter.append(<code>c</code>) works the same way as
178: * StringWriter.write(<code>c</code>).
179: *
180: * @param c
181: * The character appended to the StringWriter.
182: * @return The StringWriter.
183: */
184: @Override
185: public StringWriter append(char c) {
186: write(c);
187: return this ;
188: }
189:
190: /**
191: * Append a CharSequence <code>csq</code> to the StringWriter. The
192: * StringWriter.append(<code>csq</code>) works the same way as
193: * StringWriter.write(<code>csq</code>.toString()). If <code>csq</code>
194: * is null, then "null" will be substituted for <code>csq</code>.
195: *
196: * @param csq
197: * The CharSequence appended to the StringWriter.
198: * @return The StringWriter
199: */
200: @Override
201: public StringWriter append(CharSequence csq) {
202: if (null == csq) {
203: write(TOKEN_NULL);
204: } else {
205: write(csq.toString());
206: }
207: return this ;
208: }
209:
210: /**
211: * Append a subsequence of a CharSequence <code>csq</code> to the
212: * StringWriter. The first char and the last char of the subsequnce is
213: * specified by the parameter <code>start</code> and <code>end</code>.
214: * The StringWriter.append(<code>csq</code>) works the same way as
215: * StringWriter.write(<code>csq</code>.subSequence(<code>start</code>,<code>end</code>).toString).If
216: * <code>csq</code> is null, then "null" will be substituted for
217: * <code>csq</code>. s
218: *
219: * @param csq
220: * The CharSequence appended to the StringWriter.
221: * @param start
222: * The index of the first char in the CharSequence appended to
223: * the StringWriter.
224: * @param end
225: * The index of the char after the last one in the CharSequence
226: * appended to the StringWriter.
227: * @return The StringWriter.
228: * @throws IndexOutOfBoundsException
229: * If start is less than end, end is greater than the length of
230: * the CharSequence, or start or end is negative.
231: */
232: @Override
233: public StringWriter append(CharSequence csq, int start, int end) {
234: if (null == csq) {
235: csq = TOKEN_NULL;
236: }
237: String output = csq.subSequence(start, end).toString();
238: write(output, 0, output.length());
239: return this;
240: }
241: }
|