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.
18 package tests.support;
20 import java.io.IOException;
21 import java.io.Writer;
23 public class Support_StringWriter extends Writer {
24 private StringBuffer buf;
27 * Constructs a new StringWriter which has a StringBuffer allocated with the
28 * default size of 16 characters. The StringBuffer is also the
29 * <code>lock</code> used to synchronize access to this Writer.
31 public Support_StringWriter() {
33 buf = new StringBuffer(16);
38 * Constructs a new StringWriter which has a StringBuffer allocated with the
39 * size of <code>initialSize</code> characters. The StringBuffer is also
40 * the <code>lock</code> used to synchronize access to this Writer.
42 public Support_StringWriter(int initialSize) {
43 if (initialSize >= 0) {
44 buf = new StringBuffer(initialSize);
47 throw new IllegalArgumentException();
52 * Close this Writer. This is the concrete implementation required. This
53 * particular implementation does nothing.
55 * @exception java.io.IOException
56 * If an IO error occurs closing this StringWriter.
59 public void close() throws IOException {
63 * Flush this Writer. This is the concrete implementation required. This
64 * particular implementation does nothing.
72 * Answer the contents of this StringWriter as a StringBuffer. Any changes
73 * made to the StringBuffer by the receiver or the caller are reflected in
76 * @return this StringWriters local StringBuffer.
78 public StringBuffer getBuffer() {
85 * Answer the contents of this StringWriter as a String. Any changes made to
86 * the StringBuffer by the receiver after returning will not be reflected in
87 * the String returned to the caller.
89 * @return this StringWriters current contents as a String.
92 public String toString() {
94 return buf.toString();
99 * Writes <code>count</code> characters starting at <code>offset</code>
100 * in <code>buf</code> to this StringWriter.
103 * the non-null array containing characters to write.
105 * offset in buf to retrieve characters
107 * maximum number of characters to write
109 * @exception java.lang.ArrayIndexOutOfBoundsException
110 * If offset or count are outside of bounds.
113 public void write(char[] buf, int offset, int count) {
114 // avoid int overflow
115 if (0 <= offset && offset <= buf.length && 0 <= count
116 && count <= buf.length - offset) {
117 synchronized (lock) {
118 this.buf.append(buf, offset, count);
121 throw new ArrayIndexOutOfBoundsException();
126 * Writes the specified character <code>oneChar</code> to this
127 * StringWriter. This implementation writes the low order two bytes to the
131 * The character to write
135 public void write(int oneChar) {
136 synchronized (lock) {
137 buf.append((char) oneChar);
142 * Writes the characters from the String <code>str</code> to this
146 * the non-null String containing the characters to write.
150 public void write(String str) {
151 synchronized (lock) {
157 * Writes <code>count</code> number of characters starting at
158 * <code>offset</code> from the String <code>str</code> to this
162 * the non-null String containing the characters to write.
164 * the starting point to retrieve characters.
166 * the number of characters to retrieve and write.
168 * @exception java.lang.ArrayIndexOutOfBoundsException
169 * If offset or count are outside of bounds.
172 public void write(String str, int offset, int count) {
173 String sub = str.substring(offset, offset + count);
174 synchronized (lock) {