001: /*
002: * Copyright 1996-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package java.io;
027:
028: /**
029: * A character stream whose source is a string.
030: *
031: * @version 1.32, 07/05/05
032: * @author Mark Reinhold
033: * @since JDK1.1
034: */
035:
036: public class StringReader extends Reader {
037:
038: private String str;
039: private int length;
040: private int next = 0;
041: private int mark = 0;
042:
043: /**
044: * Creates a new string reader.
045: *
046: * @param s String providing the character stream.
047: */
048: public StringReader(String s) {
049: this .str = s;
050: this .length = s.length();
051: }
052:
053: /** Check to make sure that the stream has not been closed */
054: private void ensureOpen() throws IOException {
055: if (str == null)
056: throw new IOException("Stream closed");
057: }
058:
059: /**
060: * Reads a single character.
061: *
062: * @return The character read, or -1 if the end of the stream has been
063: * reached
064: *
065: * @exception IOException If an I/O error occurs
066: */
067: public int read() throws IOException {
068: synchronized (lock) {
069: ensureOpen();
070: if (next >= length)
071: return -1;
072: return str.charAt(next++);
073: }
074: }
075:
076: /**
077: * Reads characters into a portion of an array.
078: *
079: * @param cbuf Destination buffer
080: * @param off Offset at which to start writing characters
081: * @param len Maximum number of characters to read
082: *
083: * @return The number of characters read, or -1 if the end of the
084: * stream has been reached
085: *
086: * @exception IOException If an I/O error occurs
087: */
088: public int read(char cbuf[], int off, int len) throws IOException {
089: synchronized (lock) {
090: ensureOpen();
091: if ((off < 0) || (off > cbuf.length) || (len < 0)
092: || ((off + len) > cbuf.length) || ((off + len) < 0)) {
093: throw new IndexOutOfBoundsException();
094: } else if (len == 0) {
095: return 0;
096: }
097: if (next >= length)
098: return -1;
099: int n = Math.min(length - next, len);
100: str.getChars(next, next + n, cbuf, off);
101: next += n;
102: return n;
103: }
104: }
105:
106: /**
107: * Skips the specified number of characters in the stream. Returns
108: * the number of characters that were skipped.
109: *
110: * <p>The <code>ns</code> parameter may be negative, even though the
111: * <code>skip</code> method of the {@link Reader} superclass throws
112: * an exception in this case. Negative values of <code>ns</code> cause the
113: * stream to skip backwards. Negative return values indicate a skip
114: * backwards. It is not possible to skip backwards past the beginning of
115: * the string.
116: *
117: * <p>If the entire string has been read or skipped, then this method has
118: * no effect and always returns 0.
119: *
120: * @exception IOException If an I/O error occurs
121: */
122: public long skip(long ns) throws IOException {
123: synchronized (lock) {
124: ensureOpen();
125: if (next >= length)
126: return 0;
127: // Bound skip by beginning and end of the source
128: long n = Math.min(length - next, ns);
129: n = Math.max(-next, n);
130: next += n;
131: return n;
132: }
133: }
134:
135: /**
136: * Tells whether this stream is ready to be read.
137: *
138: * @return True if the next read() is guaranteed not to block for input
139: *
140: * @exception IOException If the stream is closed
141: */
142: public boolean ready() throws IOException {
143: synchronized (lock) {
144: ensureOpen();
145: return true;
146: }
147: }
148:
149: /**
150: * Tells whether this stream supports the mark() operation, which it does.
151: */
152: public boolean markSupported() {
153: return true;
154: }
155:
156: /**
157: * Marks the present position in the stream. Subsequent calls to reset()
158: * will reposition the stream to this point.
159: *
160: * @param readAheadLimit Limit on the number of characters that may be
161: * read while still preserving the mark. Because
162: * the stream's input comes from a string, there
163: * is no actual limit, so this argument must not
164: * be negative, but is otherwise ignored.
165: *
166: * @exception IllegalArgumentException If readAheadLimit is < 0
167: * @exception IOException If an I/O error occurs
168: */
169: public void mark(int readAheadLimit) throws IOException {
170: if (readAheadLimit < 0) {
171: throw new IllegalArgumentException("Read-ahead limit < 0");
172: }
173: synchronized (lock) {
174: ensureOpen();
175: mark = next;
176: }
177: }
178:
179: /**
180: * Resets the stream to the most recent mark, or to the beginning of the
181: * string if it has never been marked.
182: *
183: * @exception IOException If an I/O error occurs
184: */
185: public void reset() throws IOException {
186: synchronized (lock) {
187: ensureOpen();
188: next = mark;
189: }
190: }
191:
192: /**
193: * Closes the stream and releases any system resources associated with
194: * it. Once the stream has been closed, further read(),
195: * ready(), mark(), or reset() invocations will throw an IOException.
196: * Closing a previously closed stream has no effect.
197: */
198: public void close() {
199: str = null;
200: }
201: }
|