Support_StringWriter.java revision e98fbf8686c5289bf03fe5c3de7ff82d3a77104d
1e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes/*
2e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * Licensed to the Apache Software Foundation (ASF) under one or more
3e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * contributor license agreements.  See the NOTICE file distributed with
4e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * this work for additional information regarding copyright ownership.
5e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * The ASF licenses this file to You under the Apache License, Version 2.0
6e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * (the "License"); you may not use this file except in compliance with
7e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * the License.  You may obtain a copy of the License at
8e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes *
9e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes *     http://www.apache.org/licenses/LICENSE-2.0
10e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes *
11e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * Unless required by applicable law or agreed to in writing, software
12e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
13e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * See the License for the specific language governing permissions and
15e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes * limitations under the License.
16e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes */
17e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes
18e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughespackage tests.support;
19e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes
20e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughesimport java.io.IOException;
21e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughesimport java.io.Writer;
22e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes
23e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughespublic class Support_StringWriter extends Writer {
24e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	private StringBuffer buf;
25e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes
26e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	/**
27e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * Constructs a new StringWriter which has a StringBuffer allocated with the
28e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * default size of 16 characters. The StringBuffer is also the
29e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * <code>lock</code> used to synchronize access to this Writer.
30e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 */
31e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	public Support_StringWriter() {
32e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		super();
33e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		buf = new StringBuffer(16);
34e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		lock = buf;
35e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	}
36e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes
37e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	/**
38e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * Constructs a new StringWriter which has a StringBuffer allocated with the
39e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * size of <code>initialSize</code> characters. The StringBuffer is also
40e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * the <code>lock</code> used to synchronize access to this Writer.
41e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 */
42e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	public Support_StringWriter(int initialSize) {
43e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		if (initialSize >= 0) {
44e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes			buf = new StringBuffer(initialSize);
45e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes			lock = buf;
46e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		} else {
47e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes            throw new IllegalArgumentException();
48e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes        }
49e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	}
50e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes
51e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	/**
52e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * Close this Writer. This is the concrete implementation required. This
53e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * particular implementation does nothing.
54e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *
55e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * @exception java.io.IOException
56e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *                If an IO error occurs closing this StringWriter.
57e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 */
58e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	@Override
59e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes    public void close() throws IOException {
60e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	}
61e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes
62e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	/**
63e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * Flush this Writer. This is the concrete implementation required. This
64e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * particular implementation does nothing.
65e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *
66e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 */
67e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	@Override
68e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes    public void flush() {
69e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	}
70e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes
71e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	/**
72e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * Answer the contents of this StringWriter as a StringBuffer. Any changes
73e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * made to the StringBuffer by the receiver or the caller are reflected in
74e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * this StringWriter.
75e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *
76e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * @return this StringWriters local StringBuffer.
77e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 */
78e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	public StringBuffer getBuffer() {
79e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		synchronized (lock) {
80e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes			return buf;
81e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		}
82e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	}
83e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes
84e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	/**
85e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * Answer the contents of this StringWriter as a String. Any changes made to
86e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * the StringBuffer by the receiver after returning will not be reflected in
87e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * the String returned to the caller.
88e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *
89e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * @return this StringWriters current contents as a String.
90e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 */
91e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	@Override
92e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes    public String toString() {
93e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		synchronized (lock) {
94e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes			return buf.toString();
95e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		}
96e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	}
97e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes
98e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	/**
99e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * Writes <code>count</code> characters starting at <code>offset</code>
100e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * in <code>buf</code> to this StringWriter.
101e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *
102e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * @param buf
103e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *            the non-null array containing characters to write.
104e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * @param offset
105e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *            offset in buf to retrieve characters
106e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * @param count
107e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *            maximum number of characters to write
108e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *
109e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * @exception java.lang.ArrayIndexOutOfBoundsException
110e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *                If offset or count are outside of bounds.
111e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 */
112e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	@Override
113e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes    public void write(char[] buf, int offset, int count) {
114e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		// avoid int overflow
115e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		if (0 <= offset && offset <= buf.length && 0 <= count
116e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes				&& count <= buf.length - offset) {
117e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes			synchronized (lock) {
118e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes				this.buf.append(buf, offset, count);
119e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes			}
120e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		} else {
121e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes            throw new ArrayIndexOutOfBoundsException();
122e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes        }
123e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	}
124e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes
125e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	/**
126e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * Writes the specified character <code>oneChar</code> to this
127e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * StringWriter. This implementation writes the low order two bytes to the
128e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * Stream.
129e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *
130e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * @param oneChar
131e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *            The character to write
132e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *
133e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 */
134e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	@Override
135e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes    public void write(int oneChar) {
136e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		synchronized (lock) {
137e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes			buf.append((char) oneChar);
138e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		}
139e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	}
140e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes
141e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	/**
142e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * Writes the characters from the String <code>str</code> to this
143e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * StringWriter.
144e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *
145e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * @param str
146e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *            the non-null String containing the characters to write.
147e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *
148e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 */
149e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	@Override
150e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes    public void write(String str) {
151e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		synchronized (lock) {
152e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes			buf.append(str);
153e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		}
154e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	}
155e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes
156e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	/**
157e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * Writes <code>count</code> number of characters starting at
158e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * <code>offset</code> from the String <code>str</code> to this
159e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * StringWriter.
160e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *
161e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * @param str
162e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *            the non-null String containing the characters to write.
163e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * @param offset
164e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *            the starting point to retrieve characters.
165e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * @param count
166e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *            the number of characters to retrieve and write.
167e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *
168e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 * @exception java.lang.ArrayIndexOutOfBoundsException
169e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 *                If offset or count are outside of bounds.
170e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	 */
171e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	@Override
172e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes    public void write(String str, int offset, int count) {
173e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		String sub = str.substring(offset, offset + count);
174e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		synchronized (lock) {
175e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes			buf.append(sub);
176e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes		}
177e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes	}
178e98fbf8686c5289bf03fe5c3de7ff82d3a77104dElliott Hughes}
179