KickJava   Java API By Example, From Geeks To Geeks.

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


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.InputStream 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 StreamCharacterIterator implements CharacterIterator
68 {
69     /** Underlying is */
70     private final InputStream JavaDoc is;
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 InputStream, which is parsed */
79     public StreamCharacterIterator(InputStream JavaDoc is)
80     {
81         this.is = is;
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
115     /** @return a character at the specified position. */
116     public char charAt(int pos)
117     {
118         try
119         {
120             ensure(pos);
121             return buff.charAt(pos);
122         }
123         catch (IOException JavaDoc e)
124         {
125             throw new StringIndexOutOfBoundsException JavaDoc(e.getMessage());
126         }
127     }
128
129     /** @return <tt>true</tt> iff if the specified index is after the end of the character stream */
130     public boolean isEnd(int pos)
131     {
132         if (buff.length() > pos)
133         {
134             return false;
135         }
136         else
137         {
138             try
139             {
140                 ensure(pos);
141                 return (buff.length() <= pos);
142             }
143             catch (IOException JavaDoc e)
144             {
145                 throw new StringIndexOutOfBoundsException JavaDoc(e.getMessage());
146             }
147         }
148     }
149
150     /** Reads n characters from the stream and appends them to the buffer */
151     private int read(int n) throws IOException JavaDoc
152     {
153         if (closed)
154         {
155             return 0;
156         }
157
158         int c;
159         int i = n;
160         while (--i >= 0)
161         {
162             c = is.read();
163             if (c < 0) // EOF
164
{
165                 closed = true;
166                 break;
167             }
168             buff.append((char) c);
169         }
170         return n - i;
171     }
172
173     /** Reads rest of the stream. */
174     private void readAll() throws IOException JavaDoc
175     {
176         while(! closed)
177         {
178             read(1000);
179         }
180     }
181
182     /** Reads chars up to the idx */
183     private void ensure(int idx) throws IOException JavaDoc
184     {
185         if (closed)
186         {
187             return;
188         }
189
190         if (idx < buff.length())
191         {
192             return;
193         }
194
195         read(idx + 1 - buff.length());
196     }
197 }
198
Popular Tags