001 /* CharArrayReader.java -- Read an array of characters as a stream
002 Copyright (C) 1998, 2001, 2005 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package java.io;
040
041 /**
042 * This class permits an array of chars to be read as an input stream.
043 *
044 * @author Aaron M. Renn (arenn@urbanophile.com)
045 * @author Warren Levy (warrenl@cygnus.com)
046 */
047 public class CharArrayReader extends Reader
048 {
049 /**
050 * The array that contains the data supplied during read operations
051 */
052 protected char[] buf;
053
054 /**
055 * The array index of the next char to be read from the buffer
056 * <code>buf</code>
057 */
058 protected int pos;
059
060 /**
061 * The currently marked position in the stream. This defaults to 0, so a
062 * reset operation on the stream resets it to read from array index 0 in
063 * the buffer - even if the stream was initially created with an offset
064 * greater than 0
065 */
066 protected int markedPos;
067
068 /**
069 * This indicates the maximum number of chars that can be read from this
070 * stream. It is the array index of the position after the last valid
071 * char in the buffer <code>buf</code>
072 */
073 protected int count;
074
075 /**
076 * Create a new CharArrayReader that will read chars from the passed
077 * in char array. This stream will read from the beginning to the end
078 * of the array. It is identical to calling an overloaded constructor
079 * as <code>CharArrayReader(buf, 0, buf.length)</code>.
080 * <p>
081 * Note that this array is not copied. If its contents are changed
082 * while this stream is being read, those changes will be reflected in the
083 * chars supplied to the reader. Please use caution in changing the
084 * contents of the buffer while this stream is open.
085 *
086 * @param buffer The char array buffer this stream will read from.
087 */
088 public CharArrayReader(char[] buffer)
089 {
090 this(buffer, 0, buffer.length);
091 }
092
093 /**
094 * Create a new CharArrayReader that will read chars from the passed
095 * in char array. This stream will read from position
096 * <code>offset</code> in the array for a length of
097 * <code>length</code> chars past <code>offset</code>. If the
098 * stream is reset to a position before <code>offset</code> then
099 * more than <code>length</code> chars can be read from the stream.
100 * The <code>length</code> value should be viewed as the array index
101 * one greater than the last position in the buffer to read.
102 * <p>
103 * Note that this array is not copied. If its contents are changed
104 * while this stream is being read, those changes will be reflected in the
105 * chars supplied to the reader. Please use caution in changing the
106 * contents of the buffer while this stream is open.
107 *
108 * @param buffer The char array buffer this stream will read from.
109 * @param offset The index into the buffer to start reading chars from
110 * @param length The number of chars to read from the buffer
111 */
112 public CharArrayReader(char[] buffer, int offset, int length)
113 {
114 super();
115 if (offset < 0 || length < 0 || offset > buffer.length)
116 throw new IllegalArgumentException();
117
118 buf = buffer;
119
120 count = offset + length;
121 if (count > buf.length)
122 count = buf.length;
123
124 pos = offset;
125 markedPos = pos;
126 }
127
128 /**
129 * This method closes the stream.
130 */
131 public void close()
132 {
133 synchronized (lock)
134 {
135 buf = null;
136 }
137 }
138
139 /**
140 * This method sets the mark position in this stream to the current
141 * position. Note that the <code>readlimit</code> parameter in this
142 * method does nothing as this stream is always capable of
143 * remembering all the chars int it.
144 * <p>
145 * Note that in this class the mark position is set by default to
146 * position 0 in the stream. This is in constrast to some other
147 * stream types where there is no default mark position.
148 *
149 * @param readAheadLimit The number of chars this stream must
150 * remember. This parameter is ignored.
151 *
152 * @exception IOException If an error occurs
153 */
154 public void mark(int readAheadLimit) throws IOException
155 {
156 synchronized (lock)
157 {
158 if (buf == null)
159 throw new IOException("Stream closed");
160 // readAheadLimit is ignored per Java Class Lib. book, p. 318.
161 markedPos = pos;
162 }
163 }
164
165 /**
166 * This method overrides the <code>markSupported</code> method in
167 * <code>Reader</code> in order to return <code>true</code> -
168 * indicating that this stream class supports mark/reset
169 * functionality.
170 *
171 * @return <code>true</code> to indicate that this class supports
172 * mark/reset.
173 */
174 public boolean markSupported()
175 {
176 return true;
177 }
178
179 /**
180 * This method reads one char from the stream. The <code>pos</code>
181 * counter is advanced to the next char to be read. The char read
182 * is returned as an int in the range of 0-65535. If the stream
183 * position is already at the end of the buffer, no char is read and
184 * a -1 is returned in order to indicate the end of the stream.
185 *
186 * @return The char read, or -1 if end of stream
187 */
188 public int read() throws IOException
189 {
190 synchronized (lock)
191 {
192 if (buf == null)
193 throw new IOException("Stream closed");
194
195 if (pos < 0)
196 throw new ArrayIndexOutOfBoundsException(pos);
197
198 if (pos < count)
199 return ((int) buf[pos++]) & 0xFFFF;
200 return -1;
201 }
202 }
203
204 /**
205 * This method reads chars from the stream and stores them into a
206 * caller supplied buffer. It starts storing the data at index
207 * <code>offset</code> into the buffer and attempts to read
208 * <code>len</code> chars. This method can return before reading
209 * the number of chars requested if the end of the stream is
210 * encountered first. The actual number of chars read is returned.
211 * If no chars can be read because the stream is already at the end
212 * of stream position, a -1 is returned.
213 * <p>
214 * This method does not block.
215 *
216 * @param b The array into which the chars read should be stored.
217 * @param off The offset into the array to start storing chars
218 * @param len The requested number of chars to read
219 *
220 * @return The actual number of chars read, or -1 if end of stream.
221 */
222 public int read(char[] b, int off, int len) throws IOException
223 {
224 synchronized (lock)
225 {
226 if (buf == null)
227 throw new IOException("Stream closed");
228
229 /* Don't need to check pos value, arraycopy will check it. */
230 if (off < 0 || len < 0 || off + len > b.length)
231 throw new IndexOutOfBoundsException();
232
233 if (pos >= count)
234 return -1;
235
236 int numChars = Math.min(count - pos, len);
237 System.arraycopy(buf, pos, b, off, numChars);
238 pos += numChars;
239 return numChars;
240 }
241 }
242
243 /**
244 * Return true if more characters are available to be read.
245 *
246 * @return <code>true</code> to indicate that this stream is ready
247 * to be read.
248 *
249 * @specnote The JDK 1.3 API docs are wrong here. This method will
250 * return false if there are no more characters available.
251 */
252 public boolean ready() throws IOException
253 {
254 if (buf == null)
255 throw new IOException("Stream closed");
256
257 return (pos < count);
258 }
259
260 /**
261 * This method sets the read position in the stream to the mark
262 * point by setting the <code>pos</code> variable equal to the
263 * <code>mark</code> variable. Since a mark can be set anywhere in
264 * the array, the mark/reset methods int this class can be used to
265 * provide random search capabilities for this type of stream.
266 */
267 public void reset() throws IOException
268 {
269 synchronized (lock)
270 {
271 if (buf == null)
272 throw new IOException("Stream closed");
273
274 pos = markedPos;
275 }
276 }
277
278 /**
279 * This method attempts to skip the requested number of chars in the
280 * input stream. It does this by advancing the <code>pos</code> value by the
281 * specified number of chars. It this would exceed the length of the
282 * buffer, then only enough chars are skipped to position the stream at
283 * the end of the buffer. The actual number of chars skipped is returned.
284 *
285 * @param n The requested number of chars to skip
286 *
287 * @return The actual number of chars skipped.
288 */
289 public long skip(long n) throws IOException
290 {
291 synchronized (lock)
292 {
293 if (buf == null)
294 throw new IOException("Stream closed");
295
296 // Even though the var numChars is a long, in reality it can never
297 // be larger than an int since the result of subtracting 2 positive
298 // ints will always fit in an int. Since we have to return a long
299 // anyway, numChars might as well just be a long.
300 long numChars = Math.min((long) (count - pos), n < 0 ? 0L : n);
301 pos += numChars;
302 return numChars;
303 }
304 }
305 }