KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > StringReader


1 /*
2  * @(#)StringReader.java 1.24 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 /**
12  * A character stream whose source is a string.
13  *
14  * @version 1.24, 04/02/19
15  * @author Mark Reinhold
16  * @since JDK1.1
17  */

18
19 public class StringReader extends Reader JavaDoc {
20
21     private String JavaDoc str;
22     private int length;
23     private int next = 0;
24     private int mark = 0;
25
26     /**
27      * Create a new string reader.
28      *
29      * @param s String providing the character stream.
30      */

31     public StringReader(String JavaDoc s) {
32     this.str = s;
33     this.length = s.length();
34     }
35
36     /** Check to make sure that the stream has not been closed */
37     private void ensureOpen() throws IOException JavaDoc {
38     if (str == null)
39         throw new IOException JavaDoc("Stream closed");
40     }
41
42     /**
43      * Read a single character.
44      *
45      * @return The character read, or -1 if the end of the stream has been
46      * reached
47      *
48      * @exception IOException If an I/O error occurs
49      */

50     public int read() throws IOException JavaDoc {
51     synchronized (lock) {
52         ensureOpen();
53         if (next >= length)
54         return -1;
55         return str.charAt(next++);
56     }
57     }
58
59     /**
60      * Read characters into a portion of an array.
61      *
62      * @param cbuf Destination buffer
63      * @param off Offset at which to start writing characters
64      * @param len Maximum number of characters to read
65      *
66      * @return The number of characters read, or -1 if the end of the
67      * stream has been reached
68      *
69      * @exception IOException If an I/O error occurs
70      */

71     public int read(char cbuf[], int off, int len) throws IOException JavaDoc {
72     synchronized (lock) {
73         ensureOpen();
74             if ((off < 0) || (off > cbuf.length) || (len < 0) ||
75                 ((off + len) > cbuf.length) || ((off + len) < 0)) {
76                 throw new IndexOutOfBoundsException JavaDoc();
77             } else if (len == 0) {
78                 return 0;
79             }
80         if (next >= length)
81         return -1;
82         int n = Math.min(length - next, len);
83         str.getChars(next, next + n, cbuf, off);
84         next += n;
85         return n;
86     }
87     }
88
89     /**
90      * Skips the specified number of characters in the stream. Returns
91      * the number of characters that were skipped.
92      *
93      * <p>The <code>ns</code> parameter may be negative, even though the
94      * <code>skip</code> method of the {@link Reader} superclass throws
95      * an exception in this case. Negative values of <code>ns</code> cause the
96      * stream to skip backwards. Negative return values indicate a skip
97      * backwards. It is not possible to skip backwards past the beginning of
98      * the string.
99      *
100      * <p>If the entire string has been read or skipped, then this method has
101      * no effect and always returns 0.
102      *
103      * @exception IOException If an I/O error occurs
104      */

105     public long skip(long ns) throws IOException JavaDoc {
106     synchronized (lock) {
107             ensureOpen();
108             if (next >= length)
109                 return 0;
110             // Bound skip by beginning and end of the source
111
long n = Math.min(length - next, ns);
112             n = Math.max(-next, n);
113             next += n;
114             return n;
115         }
116     }
117
118     /**
119      * Tell whether this stream is ready to be read.
120      *
121      * @return True if the next read() is guaranteed not to block for input
122      *
123      * @exception IOException If the stream is closed
124      */

125     public boolean ready() throws IOException JavaDoc {
126         synchronized (lock) {
127         ensureOpen();
128         return true;
129         }
130     }
131
132     /**
133      * Tell whether this stream supports the mark() operation, which it does.
134      */

135     public boolean markSupported() {
136     return true;
137     }
138
139     /**
140      * Mark the present position in the stream. Subsequent calls to reset()
141      * will reposition the stream to this point.
142      *
143      * @param readAheadLimit Limit on the number of characters that may be
144      * read while still preserving the mark. Because
145      * the stream's input comes from a string, there
146      * is no actual limit, so this argument must not
147      * be negative, but is otherwise ignored.
148      *
149      * @exception IllegalArgumentException If readAheadLimit is < 0
150      * @exception IOException If an I/O error occurs
151      */

152     public void mark(int readAheadLimit) throws IOException JavaDoc {
153     if (readAheadLimit < 0){
154         throw new IllegalArgumentException JavaDoc("Read-ahead limit < 0");
155     }
156     synchronized (lock) {
157         ensureOpen();
158         mark = next;
159     }
160     }
161
162     /**
163      * Reset the stream to the most recent mark, or to the beginning of the
164      * string if it has never been marked.
165      *
166      * @exception IOException If an I/O error occurs
167      */

168     public void reset() throws IOException JavaDoc {
169     synchronized (lock) {
170         ensureOpen();
171         next = mark;
172     }
173     }
174
175     /**
176      * Close the stream.
177      */

178     public void close() {
179     str = null;
180     }
181
182 }
183
Popular Tags