1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.regexp;
19
20 import java.io.Reader;
21 import java.io.IOException;
22
23 /**
24 * Encapsulates java.io.Reader as CharacterIterator
25 *
26 * @author <a href="mailto:ales.novak@netbeans.com">Ales Novak</a>
27 * @version CVS $Id: ReaderCharacterIterator.java 518156 2007-03-14 14:31:26Z vgritsenko $
28 */
29 public final class ReaderCharacterIterator implements CharacterIterator
30 {
31 /** Underlying reader */
32 private final Reader reader;
33
34 /** Buffer of read chars */
35 private final StringBuffer buff;
36
37 /** read end? */
38 private boolean closed;
39
40 /** @param reader a Reader, which is parsed */
41 public ReaderCharacterIterator(Reader reader)
42 {
43 this.reader = reader;
44 this.buff = new StringBuffer(512);
45 this.closed = false;
46 }
47
48 /** @return a substring */
49 public String substring(int beginIndex, int endIndex)
50 {
51 try
52 {
53 ensure(endIndex);
54 return buff.toString().substring(beginIndex, endIndex);
55 }
56 catch (IOException e)
57 {
58 throw new StringIndexOutOfBoundsException(e.getMessage());
59 }
60 }
61
62 /** @return a substring */
63 public String substring(int beginIndex)
64 {
65 try
66 {
67 readAll();
68 return buff.toString().substring(beginIndex);
69 }
70 catch (IOException e)
71 {
72 throw new StringIndexOutOfBoundsException(e.getMessage());
73 }
74 }
75
76 /** @return a character at the specified position. */
77 public char charAt(int pos)
78 {
79 try
80 {
81 ensure(pos);
82 return buff.charAt(pos);
83 }
84 catch (IOException e)
85 {
86 throw new StringIndexOutOfBoundsException(e.getMessage());
87 }
88 }
89
90 /** @return <tt>true</tt> iff if the specified index is after the end of the character stream */
91 public boolean isEnd(int pos)
92 {
93 if (buff.length() > pos)
94 {
95 return false;
96 }
97 else
98 {
99 try
100 {
101 ensure(pos);
102 return (buff.length() <= pos);
103 }
104 catch (IOException e)
105 {
106 throw new StringIndexOutOfBoundsException(e.getMessage());
107 }
108 }
109 }
110
111 /** Reads n characters from the stream and appends them to the buffer */
112 private int read(int n) throws IOException
113 {
114 if (closed)
115 {
116 return 0;
117 }
118
119 char[] c = new char[n];
120 int count = 0;
121 int read = 0;
122
123 do
124 {
125 read = reader.read(c);
126 if (read < 0) // EOF
127 {
128 closed = true;
129 break;
130 }
131 count += read;
132 buff.append(c, 0, read);
133 }
134 while (count < n);
135
136 return count;
137 }
138
139 /** Reads rest of the stream. */
140 private void readAll() throws IOException
141 {
142 while(! closed)
143 {
144 read(1000);
145 }
146 }
147
148 /** Reads chars up to the idx */
149 private void ensure(int idx) throws IOException
150 {
151 if (closed)
152 {
153 return;
154 }
155
156 if (idx < buff.length())
157 {
158 return;
159 }
160 read(idx + 1 - buff.length());
161 }
162 }