KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > regexp > internal > ReaderCharacterIterator


1 package com.sun.org.apache.regexp.internal;
2
3 /*
4  * ====================================================================
5  *
6  * The Apache Software License, Version 1.1
7  *
8  * Copyright (c) 1999 The Apache Software Foundation. All rights
9  * reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution, if
24  * any, must include the following acknowlegement:
25  * "This product includes software developed by the
26  * Apache Software Foundation (http://www.apache.org/)."
27  * Alternately, this acknowlegement may appear in the software itself,
28  * if and wherever such third-party acknowlegements normally appear.
29  *
30  * 4. The names "The Jakarta Project", "Jakarta-Regexp", and "Apache Software
31  * Foundation" must not be used to endorse or promote products derived
32  * from this software without prior written permission. For written
33  * permission, please contact apache@apache.org.
34  *
35  * 5. Products derived from this software may not be called "Apache"
36  * nor may "Apache" appear in their names without prior written
37  * permission of the Apache Group.
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This software consists of voluntary contributions made by many
54  * individuals on behalf of the Apache Software Foundation. For more
55  * information on the Apache Software Foundation, please see
56  * <http://www.apache.org/>.
57  *
58  */

59
60 import java.io.Reader JavaDoc;
61 import java.io.IOException JavaDoc;
62
63 /** Encapsulates InputStream, ...
64  *
65  * @author <a HREF="mailto:ales.novak@netbeans.com">Ales Novak</a>
66  */

67 public final class ReaderCharacterIterator implements CharacterIterator
68 {
69     /** Underlying is */
70     private final Reader JavaDoc reader;
71
72     /** Buffer of read chars */
73     private final StringBuffer JavaDoc buff;
74
75     /** read end? */
76     private boolean closed;
77
78     /** @param is an Reader, which is parsed */
79     public ReaderCharacterIterator(Reader JavaDoc reader)
80     {
81         this.reader = reader;
82         this.buff = new StringBuffer JavaDoc(512);
83         this.closed = false;
84     }
85
86     /** @return a substring */
87     public String JavaDoc substring(int offset, int length)
88     {
89         try
90         {
91             ensure(offset + length);
92             return buff.toString().substring(offset, length);
93         }
94         catch (IOException JavaDoc e)
95         {
96             throw new StringIndexOutOfBoundsException JavaDoc(e.getMessage());
97         }
98     }
99
100     /** @return a substring */
101     public String JavaDoc substring(int offset)
102     {
103         try
104         {
105             readAll();
106             return buff.toString().substring(offset);
107         }
108         catch (IOException JavaDoc e)
109         {
110             throw new StringIndexOutOfBoundsException JavaDoc(e.getMessage());
111         }
112     }
113
114     /** @return a character at the specified position. */
115     public char charAt(int pos)
116     {
117         try
118         {
119             ensure(pos);
120             return buff.charAt(pos);
121         }
122         catch (IOException JavaDoc e)
123         {
124             throw new StringIndexOutOfBoundsException JavaDoc(e.getMessage());
125         }
126     }
127
128     /** @return <tt>true</tt> iff if the specified index is after the end of the character stream */
129     public boolean isEnd(int pos)
130     {
131         if (buff.length() > pos)
132         {
133             return false;
134         }
135         else
136         {
137             try
138             {
139                 ensure(pos);
140                 return (buff.length() <= pos);
141             }
142             catch (IOException JavaDoc e)
143             {
144                 throw new StringIndexOutOfBoundsException JavaDoc(e.getMessage());
145             }
146         }
147     }
148
149     /** Reads n characters from the stream and appends them to the buffer */
150     private int read(int n) throws IOException JavaDoc
151     {
152         if (closed)
153         {
154             return 0;
155         }
156
157         char[] c = new char[n];
158         int count = 0;
159         int read = 0;
160
161         do
162         {
163             read = reader.read(c);
164             if (read < 0) // EOF
165
{
166                 closed = true;
167                 break;
168             }
169             count += read;
170             buff.append(c, 0, read);
171         }
172         while (count < n);
173
174         return count;
175     }
176
177     /** Reads rest of the stream. */
178     private void readAll() throws IOException JavaDoc
179     {
180         while(! closed)
181         {
182             read(1000);
183         }
184     }
185
186     /** Reads chars up to the idx */
187     private void ensure(int idx) throws IOException JavaDoc
188     {
189         if (closed)
190         {
191             return;
192         }
193
194         if (idx < buff.length())
195         {
196             return;
197         }
198         read(idx + 1 - buff.length());
199     }
200 }
201
Popular Tags