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.Reader;
23 public class Support_StringReader extends Reader {
26 private int markpos = -1;
33 * Construct a StringReader on the String <code>str</code>. The size of
34 * the reader is set to the <code>length()</code> of the String and the
35 * Object to synchronize access through is set to <code>str</code>.
38 * the String to filter reads on.
40 public Support_StringReader(String str) {
43 this.count = str.length();
47 * This method closes this StringReader. Once it is closed, you can no
48 * longer read from it. Only the first invocation of this method has any
62 * Answer a boolean indicating whether or not this StringReader is open.
64 private boolean isOpen() {
69 * Set a Mark position in this Reader. The parameter <code>readLimit</code>
70 * is ignored for StringReaders. Sending reset() will reposition the reader
71 * back to the marked position provided the mark has not been invalidated.
74 * ignored for StringReaders.
76 * @exception java.io.IOException
77 * If an error occurs attempting mark this StringReader.
80 public void mark(int readLimit) throws IOException {
86 throw new IOException("StringReader is closed");
90 throw new IllegalArgumentException();
95 * Answers a boolean indicating whether or not this StringReader supports
96 * mark() and reset(). This method always returns true.
98 * @return <code>true</code> if mark() and reset() are supported,
99 * <code>false</code> otherwise. This implementation always
100 * returns <code>true</code>.
103 public boolean markSupported() {
108 * Reads a single character from this StringReader and returns the result as
109 * an int. The 2 higher-order bytes are set to 0. If the end of reader was
110 * encountered then return -1.
112 * @return the character read or -1 if end of reader.
114 * @exception java.io.IOException
115 * If the StringReader is already closed.
118 public int read() throws IOException {
119 synchronized (lock) {
122 return str.charAt(pos++);
126 throw new IOException("StringReader is closed");
131 * Reads at most <code>count</code> characters from this StringReader and
132 * stores them at <code>offset</code> in the character array
133 * <code>buf</code>. Returns the number of characters actually read or -1
134 * if the end of reader was encountered.
137 * character array to store the read characters
139 * offset in buf to store the read characters
141 * maximum number of characters to read
142 * @return the number of characters read or -1 if end of reader.
144 * @exception java.io.IOException
145 * If the StringReader is closed.
148 public int read(char buf[], int offset, int count) throws IOException {
149 // avoid int overflow
150 if (0 <= offset && offset <= buf.length && 0 <= count
151 && count <= buf.length - offset) {
152 synchronized (lock) {
154 if (pos == this.count) {
157 int end = pos + count > this.count ? this.count : pos
159 str.getChars(pos, end, buf, offset);
160 int read = end - pos;
164 throw new IOException("StringReader is closed");
167 throw new ArrayIndexOutOfBoundsException();
171 * Answers a <code>boolean</code> indicating whether or not this
172 * StringReader is ready to be read without blocking. If the result is
173 * <code>true</code>, the next <code>read()</code> will not block. If
174 * the result is <code>false</code> this Reader may or may not block when
175 * <code>read()</code> is sent. The implementation in StringReader always
176 * returns <code>true</code> even when it has been closed.
178 * @return <code>true</code> if the receiver will not block when
179 * <code>read()</code> is called, <code>false</code> if unknown
180 * or blocking will occur.
182 * @exception java.io.IOException
183 * If an IO error occurs.
186 public boolean ready() throws IOException {
187 synchronized (lock) {
191 throw new IOException("StringReader is closed");
196 * Reset this StringReader's position to the last <code>mark()</code>
197 * location. Invocations of <code>read()/skip()</code> will occur from
198 * this new location. If this Reader was not marked, the StringReader is
199 * reset to the beginning of the String.
201 * @exception java.io.IOException
202 * If this StringReader has already been closed.
205 public void reset() throws IOException {
206 synchronized (lock) {
208 pos = markpos != -1 ? markpos : 0;
210 throw new IOException("StringReader is closed");
216 * Skips <code>count</code> number of characters in this StringReader.
217 * Subsequent <code>read()</code>'s will not return these characters
218 * unless <code>reset()</code> is used.
221 * The number of characters to skip.
222 * @return the number of characters actually skipped.
224 * @exception java.io.IOException
225 * If this StringReader has already been closed.
228 public long skip(long count) throws IOException {
229 synchronized (lock) {
235 if (count < this.count - pos) {
236 pos = pos + (int) count;
239 skipped = this.count - pos;
244 throw new IOException("StringReader is closed");