| 1 | /* StringReader.java -- permits a String to be read as a character input stream
|
|---|
| 2 | Copyright (C) 1998, 1999, 2000 Free Software Foundation
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 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)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 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.
|
|---|
| 15 |
|
|---|
| 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
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 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
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 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. */
|
|---|
| 37 |
|
|---|
| 38 | package java.io;
|
|---|
| 39 |
|
|---|
| 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
|
|---|
| 44 | */
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * This class permits a <code>String</code> to be read as a character
|
|---|
| 48 | * input stream.
|
|---|
| 49 | * <p>
|
|---|
| 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>.
|
|---|
| 53 | *
|
|---|
| 54 | * @version 0.0
|
|---|
| 55 | *
|
|---|
| 56 | * @author Aaron M. Renn ([email protected])
|
|---|
| 57 | * @author Warren Levy <[email protected]>
|
|---|
| 58 | * @date October 19, 1998.
|
|---|
| 59 | */
|
|---|
| 60 | public class StringReader extends Reader
|
|---|
| 61 | {
|
|---|
| 62 | /* A String provided by the creator of the stream. */
|
|---|
| 63 | private String buf;
|
|---|
| 64 |
|
|---|
| 65 | /* Position of the next char in buf to be read. */
|
|---|
| 66 | private int pos;
|
|---|
| 67 |
|
|---|
| 68 | /* The currently marked position in the stream. */
|
|---|
| 69 | private int markedPos;
|
|---|
| 70 |
|
|---|
| 71 | /* The index in buf one greater than the last valid character. */
|
|---|
| 72 | private int count;
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 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>.
|
|---|
| 78 | *
|
|---|
| 79 | * @param s The <code>String</code> this stream will read from.
|
|---|
| 80 | */
|
|---|
| 81 | public StringReader(String buffer)
|
|---|
| 82 | {
|
|---|
| 83 | super();
|
|---|
| 84 | buf = buffer;
|
|---|
| 85 |
|
|---|
| 86 | count = buffer.length();
|
|---|
| 87 | markedPos = pos = 0;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | public void close()
|
|---|
| 91 | {
|
|---|
| 92 | synchronized (lock)
|
|---|
| 93 | {
|
|---|
| 94 | buf = null;
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | public void mark(int readAheadLimit) throws IOException
|
|---|
| 99 | {
|
|---|
| 100 | synchronized (lock)
|
|---|
| 101 | {
|
|---|
| 102 | if (buf == null)
|
|---|
| 103 | throw new IOException("Stream closed");
|
|---|
| 104 |
|
|---|
| 105 | // readAheadLimit is ignored per Java Class Lib. book, p. 1692.
|
|---|
| 106 | markedPos = pos;
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | public boolean markSupported()
|
|---|
| 111 | {
|
|---|
| 112 | return true;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | public int read() throws IOException
|
|---|
| 116 | {
|
|---|
| 117 | synchronized (lock)
|
|---|
| 118 | {
|
|---|
| 119 | if (buf == null)
|
|---|
| 120 | throw new IOException("Stream closed");
|
|---|
| 121 |
|
|---|
| 122 | if (pos < count)
|
|---|
| 123 | return ((int) buf.charAt(pos++)) & 0xFFFF;
|
|---|
| 124 | return -1;
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | public int read(char[] b, int off, int len) throws IOException
|
|---|
| 129 | {
|
|---|
| 130 | synchronized (lock)
|
|---|
| 131 | {
|
|---|
| 132 | if (buf == null)
|
|---|
| 133 | throw new IOException("Stream closed");
|
|---|
| 134 |
|
|---|
| 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();
|
|---|
| 138 |
|
|---|
| 139 | if (pos >= count)
|
|---|
| 140 | return -1;
|
|---|
| 141 |
|
|---|
| 142 | int lastChar = Math.min(count, pos + len);
|
|---|
| 143 | buf.getChars(pos, lastChar, b, off);
|
|---|
| 144 | int numChars = lastChar - pos;
|
|---|
| 145 | pos = lastChar;
|
|---|
| 146 | return numChars;
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 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
|
|---|
| 154 | * thrown.
|
|---|
| 155 | *
|
|---|
| 156 | * @return <code>true</code> to indicate that this object is ready to be read.
|
|---|
| 157 | * @exception IOException If the stream is closed.
|
|---|
| 158 | */
|
|---|
| 159 | public boolean ready() throws IOException
|
|---|
| 160 | {
|
|---|
| 161 | if (buf == null)
|
|---|
| 162 | throw new IOException("Stream closed");
|
|---|
| 163 |
|
|---|
| 164 | return true;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /**
|
|---|
| 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.
|
|---|
| 171 | */
|
|---|
| 172 | public void reset() throws IOException
|
|---|
| 173 | {
|
|---|
| 174 | synchronized (lock)
|
|---|
| 175 | {
|
|---|
| 176 | if (buf == null)
|
|---|
| 177 | throw new IOException("Stream closed");
|
|---|
| 178 |
|
|---|
| 179 | pos = markedPos;
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /**
|
|---|
| 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.
|
|---|
| 189 | *
|
|---|
| 190 | * @param num_chars The requested number of chars to skip
|
|---|
| 191 | *
|
|---|
| 192 | * @return The actual number of chars skipped.
|
|---|
| 193 | */
|
|---|
| 194 | public long skip(long n) throws IOException
|
|---|
| 195 | {
|
|---|
| 196 | synchronized (lock)
|
|---|
| 197 | {
|
|---|
| 198 | if (buf == null)
|
|---|
| 199 | throw new IOException("Stream closed");
|
|---|
| 200 |
|
|---|
| 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);
|
|---|
| 206 | pos += numChars;
|
|---|
| 207 | return numChars;
|
|---|
| 208 | }
|
|---|
| 209 | }
|
|---|
| 210 | }
|
|---|