1 /* StringReader.java -- permits a String to be read as a character input stream
2 Copyright (C) 1998, 1999, 2000, 2003 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>.
54 * @author Aaron M. Renn (arenn@urbanophile.com)
55 * @author Warren Levy <warrenl@cygnus.com>
56 * @date October 19, 1998.
58 public class StringReader
extends Reader
60 /* A String provided by the creator of the stream. */
63 /* Position of the next char in buf to be read. */
66 /* The currently marked position in the stream. */
67 private int markedPos
;
69 /* The index in buf one greater than the last valid character. */
73 * Create a new <code>StringReader</code> that will read chars from the
74 * passed in <code>String</code>. This stream will read from the beginning
75 * to the end of the <code>String</code>.
77 * @param s The <code>String</code> this stream will read from.
79 public StringReader(String buffer
)
84 count
= buffer
.length();
96 public void mark(int readAheadLimit
) throws IOException
101 throw new IOException("Stream closed");
103 // readAheadLimit is ignored per Java Class Lib. book, p. 1692.
108 public boolean markSupported()
113 public int read() throws IOException
118 throw new IOException("Stream closed");
121 return ((int) buf
.charAt(pos
++)) & 0xFFFF;
126 public int read(char[] b
, int off
, int len
) throws IOException
131 throw new IOException("Stream closed");
133 /* Don't need to check pos value, arraycopy will check it. */
134 if (off
< 0 || len
< 0 || off
+ len
> b
.length
)
135 throw new ArrayIndexOutOfBoundsException();
140 int lastChar
= Math
.min(count
, pos
+ len
);
141 buf
.getChars(pos
, lastChar
, b
, off
);
142 int numChars
= lastChar
- pos
;
149 * This method determines if the stream is ready to be read. This class
150 * is always ready to read and so always returns <code>true</code>, unless
151 * close() has previously been called in which case an IOException is
154 * @return <code>true</code> to indicate that this object is ready to be read.
155 * @exception IOException If the stream is closed.
157 public boolean ready() throws IOException
160 throw new IOException("Stream closed");
166 * Sets the read position in the stream to the previously
167 * marked position or to 0 (i.e., the beginning of the stream) if the mark
168 * has not already been set.
170 public void reset() throws IOException
175 throw new IOException("Stream closed");
182 * This method attempts to skip the requested number of chars in the
183 * input stream. It does this by advancing the <code>pos</code> value by
184 * the specified number of chars. It this would exceed the length of the
185 * buffer, then only enough chars are skipped to position the stream at
186 * the end of the buffer. The actual number of chars skipped is returned.
188 * @param num_chars The requested number of chars to skip
190 * @return The actual number of chars skipped.
192 public long skip(long n
) throws IOException
197 throw new IOException("Stream closed");
199 // Even though the var numChars is a long, in reality it can never
200 // be larger than an int since the result of subtracting 2 positive
201 // ints will always fit in an int. Since we have to return a long
202 // anyway, numChars might as well just be a long.
203 long numChars
= Math
.min((long) (count
- pos
), n
< 0 ?
0L : n
);