1 /* StringReader.java -- permits a String to be read as a character input stream
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
40 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
41 * "The Java Language Specification", ISBN 0-201-63451-1
42 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
43 * Status: Believed complete and correct
47 * This class permits a <code>String</code> to be read as a character
50 * The mark/reset functionality in this class behaves differently than
51 * normal. If no mark has been set, then calling the <code>reset()</code>
52 * method rewinds the read pointer to the beginning of the <code>String</code>.
56 * @author Aaron M. Renn (arenn@urbanophile.com)
57 * @author Warren Levy <warrenl@cygnus.com>
58 * @date October 19, 1998.
60 public class StringReader
extends Reader
62 /* A String provided by the creator of the stream. */
65 /* Position of the next char in buf to be read. */
68 /* The currently marked position in the stream. */
69 private int markedPos
;
71 /* The index in buf one greater than the last valid character. */
75 * Create a new <code>StringReader</code> that will read chars from the
76 * passed in <code>String</code>. This stream will read from the beginning to the
77 * end of the <code>String</code>.
79 * @param s The <code>String</code> this stream will read from.
81 public StringReader(String buffer
)
86 count
= buffer
.length();
98 public void mark(int readAheadLimit
) throws IOException
103 throw new IOException("Stream closed");
105 // readAheadLimit is ignored per Java Class Lib. book, p. 1692.
110 public boolean markSupported()
115 public int read() throws IOException
120 throw new IOException("Stream closed");
123 return ((int) buf
.charAt(pos
++)) & 0xFFFF;
128 public int read(char[] b
, int off
, int len
) throws IOException
133 throw new IOException("Stream closed");
135 /* Don't need to check pos value, arraycopy will check it. */
136 if (off
< 0 || len
< 0 || off
+ len
> b
.length
)
137 throw new ArrayIndexOutOfBoundsException();
142 int lastChar
= Math
.min(count
, pos
+ len
);
143 buf
.getChars(pos
, lastChar
, b
, off
);
144 int numChars
= lastChar
- pos
;
151 * This method determines if the stream is ready to be read. This class
152 * is always ready to read and so always returns <code>true</code>, unless
153 * close() has previously been called in which case an IOException is
156 * @return <code>true</code> to indicate that this object is ready to be read.
157 * @exception IOException If the stream is closed.
159 public boolean ready() throws IOException
162 throw new IOException("Stream closed");
168 * Sets the read position in the stream to the previously
169 * marked position or to 0 (i.e., the beginning of the stream) if the mark
170 * has not already been set.
172 public void reset() throws IOException
177 throw new IOException("Stream closed");
184 * This method attempts to skip the requested number of chars in the
185 * input stream. It does this by advancing the <code>pos</code> value by
186 * the specified number of chars. It this would exceed the length of the
187 * buffer, then only enough chars are skipped to position the stream at
188 * the end of the buffer. The actual number of chars skipped is returned.
190 * @param num_chars The requested number of chars to skip
192 * @return The actual number of chars skipped.
194 public long skip(long n
) throws IOException
199 throw new IOException("Stream closed");
201 // Even though the var numChars is a long, in reality it can never
202 // be larger than an int since the result of subtracting 2 positive
203 // ints will always fit in an int. Since we have to return a long
204 // anyway, numChars might as well just be a long.
205 long numChars
= Math
.min((long) (count
- pos
), n
< 0 ?
0L : n
);