Support_StringWriter.java revision e98fbf8686c5289bf03fe5c3de7ff82d3a77104d
1/*
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
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18package tests.support;
19
20import java.io.IOException;
21import java.io.Writer;
22
23public class Support_StringWriter extends Writer {
24	private StringBuffer buf;
25
26	/**
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.
30	 */
31	public Support_StringWriter() {
32		super();
33		buf = new StringBuffer(16);
34		lock = buf;
35	}
36
37	/**
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.
41	 */
42	public Support_StringWriter(int initialSize) {
43		if (initialSize >= 0) {
44			buf = new StringBuffer(initialSize);
45			lock = buf;
46		} else {
47            throw new IllegalArgumentException();
48        }
49	}
50
51	/**
52	 * Close this Writer. This is the concrete implementation required. This
53	 * particular implementation does nothing.
54	 *
55	 * @exception java.io.IOException
56	 *                If an IO error occurs closing this StringWriter.
57	 */
58	@Override
59    public void close() throws IOException {
60	}
61
62	/**
63	 * Flush this Writer. This is the concrete implementation required. This
64	 * particular implementation does nothing.
65	 *
66	 */
67	@Override
68    public void flush() {
69	}
70
71	/**
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
74	 * this StringWriter.
75	 *
76	 * @return this StringWriters local StringBuffer.
77	 */
78	public StringBuffer getBuffer() {
79		synchronized (lock) {
80			return buf;
81		}
82	}
83
84	/**
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.
88	 *
89	 * @return this StringWriters current contents as a String.
90	 */
91	@Override
92    public String toString() {
93		synchronized (lock) {
94			return buf.toString();
95		}
96	}
97
98	/**
99	 * Writes <code>count</code> characters starting at <code>offset</code>
100	 * in <code>buf</code> to this StringWriter.
101	 *
102	 * @param buf
103	 *            the non-null array containing characters to write.
104	 * @param offset
105	 *            offset in buf to retrieve characters
106	 * @param count
107	 *            maximum number of characters to write
108	 *
109	 * @exception java.lang.ArrayIndexOutOfBoundsException
110	 *                If offset or count are outside of bounds.
111	 */
112	@Override
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);
119			}
120		} else {
121            throw new ArrayIndexOutOfBoundsException();
122        }
123	}
124
125	/**
126	 * Writes the specified character <code>oneChar</code> to this
127	 * StringWriter. This implementation writes the low order two bytes to the
128	 * Stream.
129	 *
130	 * @param oneChar
131	 *            The character to write
132	 *
133	 */
134	@Override
135    public void write(int oneChar) {
136		synchronized (lock) {
137			buf.append((char) oneChar);
138		}
139	}
140
141	/**
142	 * Writes the characters from the String <code>str</code> to this
143	 * StringWriter.
144	 *
145	 * @param str
146	 *            the non-null String containing the characters to write.
147	 *
148	 */
149	@Override
150    public void write(String str) {
151		synchronized (lock) {
152			buf.append(str);
153		}
154	}
155
156	/**
157	 * Writes <code>count</code> number of characters starting at
158	 * <code>offset</code> from the String <code>str</code> to this
159	 * StringWriter.
160	 *
161	 * @param str
162	 *            the non-null String containing the characters to write.
163	 * @param offset
164	 *            the starting point to retrieve characters.
165	 * @param count
166	 *            the number of characters to retrieve and write.
167	 *
168	 * @exception java.lang.ArrayIndexOutOfBoundsException
169	 *                If offset or count are outside of bounds.
170	 */
171	@Override
172    public void write(String str, int offset, int count) {
173		String sub = str.substring(offset, offset + count);
174		synchronized (lock) {
175			buf.append(sub);
176		}
177	}
178}
179