KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > CharArrayReader


1 /*
2  * @(#)CharArrayReader.java 1.22 04/02/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10 /**
11  * This class implements a character buffer that can be used as a
12  * character-input stream.
13  *
14  * @author Herb Jellinek
15  * @version 1.22, 02/19/04
16  * @since JDK1.1
17  */

18 public
19 class CharArrayReader extends Reader JavaDoc {
20     /** The character buffer. */
21     protected char buf[];
22
23     /** The current buffer position. */
24     protected int pos;
25
26     /** The position of mark in buffer. */
27     protected int markedPos = 0;
28
29     /**
30      * The index of the end of this buffer. There is not valid
31      * data at or beyond this index.
32      */

33     protected int count;
34
35     /**
36      * Create an CharArrayReader from the specified array of chars.
37      * @param buf Input buffer (not copied)
38      */

39     public CharArrayReader(char buf[]) {
40     this.buf = buf;
41         this.pos = 0;
42     this.count = buf.length;
43     }
44
45     /**
46      * Create an CharArrayReader from the specified array of chars.
47      *
48      * <p> The resulting reader will start reading at the given
49      * <tt>offset</tt>. The total number of <tt>char</tt> values that can be
50      * read from this reader will be either <tt>length</tt> or
51      * <tt>buf.length-offset<tt>, whichever is smaller.
52      *
53      * @throws IllegalArgumentException
54      * If <tt>offset</tt> is negative or greater than
55      * <tt>buf.length</tt>, or if <tt>length</tt> is negative, or if
56      * the sum of these two values is negative.
57      *
58      * @param buf Input buffer (not copied)
59      * @param offset Offset of the first char to read
60      * @param length Number of chars to read
61      */

62     public CharArrayReader(char buf[], int offset, int length) {
63     if ((offset < 0) || (offset > buf.length) || (length < 0) ||
64             ((offset + length) < 0)) {
65         throw new IllegalArgumentException JavaDoc();
66     }
67     this.buf = buf;
68         this.pos = offset;
69     this.count = Math.min(offset + length, buf.length);
70         this.markedPos = offset;
71     }
72
73     /** Check to make sure that the stream has not been closed */
74     private void ensureOpen() throws IOException JavaDoc {
75     if (buf == null)
76         throw new IOException JavaDoc("Stream closed");
77     }
78
79     /**
80      * Read a single character.
81      *
82      * @exception IOException If an I/O error occurs
83      */

84     public int read() throws IOException JavaDoc {
85     synchronized (lock) {
86         ensureOpen();
87         if (pos >= count)
88         return -1;
89         else
90         return buf[pos++];
91     }
92     }
93
94     /**
95      * Read characters into a portion of an array.
96      * @param b Destination buffer
97      * @param off Offset at which to start storing characters
98      * @param len Maximum number of characters to read
99      * @return The actual number of characters read, or -1 if
100      * the end of the stream has been reached
101      *
102      * @exception IOException If an I/O error occurs
103      */

104     public int read(char b[], int off, int len) throws IOException JavaDoc {
105     synchronized (lock) {
106         ensureOpen();
107             if ((off < 0) || (off > b.length) || (len < 0) ||
108                 ((off + len) > b.length) || ((off + len) < 0)) {
109                 throw new IndexOutOfBoundsException JavaDoc();
110             } else if (len == 0) {
111                 return 0;
112             }
113
114         if (pos >= count) {
115         return -1;
116         }
117         if (pos + len > count) {
118         len = count - pos;
119         }
120         if (len <= 0) {
121         return 0;
122         }
123         System.arraycopy(buf, pos, b, off, len);
124         pos += len;
125         return len;
126     }
127     }
128
129     /**
130      * Skip characters. Returns the number of characters that were skipped.
131      *
132      * <p>The <code>n</code> parameter may be negative, even though the
133      * <code>skip</code> method of the {@link Reader} superclass throws
134      * an exception in this case. If <code>n</code> is negative, then
135      * this method does nothing and returns <code>0</code>.
136      *
137      * @param n The number of characters to skip
138      * @return The number of characters actually skipped
139      * @exception IOException If the stream is closed, or an I/O error occurs
140      */

141     public long skip(long n) throws IOException JavaDoc {
142     synchronized (lock) {
143         ensureOpen();
144         if (pos + n > count) {
145         n = count - pos;
146         }
147         if (n < 0) {
148         return 0;
149         }
150         pos += n;
151         return n;
152     }
153     }
154
155     /**
156      * Tell whether this stream is ready to be read. Character-array readers
157      * are always ready to be read.
158      *
159      * @exception IOException If an I/O error occurs
160      */

161     public boolean ready() throws IOException JavaDoc {
162     synchronized (lock) {
163         ensureOpen();
164         return (count - pos) > 0;
165     }
166     }
167
168     /**
169      * Tell whether this stream supports the mark() operation, which it does.
170      */

171     public boolean markSupported() {
172     return true;
173     }
174
175     /**
176      * Mark the present position in the stream. Subsequent calls to reset()
177      * will reposition the stream to this point.
178      *
179      * @param readAheadLimit Limit on the number of characters that may be
180      * read while still preserving the mark. Because
181      * the stream's input comes from a character array,
182      * there is no actual limit; hence this argument is
183      * ignored.
184      *
185      * @exception IOException If an I/O error occurs
186      */

187     public void mark(int readAheadLimit) throws IOException JavaDoc {
188     synchronized (lock) {
189         ensureOpen();
190         markedPos = pos;
191     }
192     }
193
194     /**
195      * Reset the stream to the most recent mark, or to the beginning if it has
196      * never been marked.
197      *
198      * @exception IOException If an I/O error occurs
199      */

200     public void reset() throws IOException JavaDoc {
201     synchronized (lock) {
202         ensureOpen();
203         pos = markedPos;
204     }
205     }
206
207     /**
208      * Close the stream.
209      */

210     public void close() {
211     buf = null;
212     }
213 }
214
Popular Tags