libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / gnu / java / util / regex / CharIndexedInputStream.java
blobe42710b5de1447ddfb3ab85b53ceeda8cf95bf5b
1 /* gnu/regexp/CharIndexedInputStream.java
2 Copyright (C) 1998-2001, 2004, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.java.util.regex;
39 import java.io.BufferedInputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
43 // TODO: move(x) shouldn't rely on calling next() x times
45 class CharIndexedInputStream implements CharIndexed
47 private static final int BUFFER_INCREMENT = 1024;
48 private static final int UNKNOWN = Integer.MAX_VALUE; // value for end
50 private BufferedInputStream br;
52 // so that we don't try to reset() right away
53 private int index = -1;
55 private int bufsize = BUFFER_INCREMENT;
57 private int end = UNKNOWN;
59 private char cached = OUT_OF_BOUNDS;
61 // Big enough for a \r\n pair
62 // lookBehind[0] = most recent
63 // lookBehind[1] = second most recent
64 private char[] lookBehind = new char[]{ OUT_OF_BOUNDS, OUT_OF_BOUNDS };
66 CharIndexedInputStream (InputStream str, int index)
68 if (str instanceof BufferedInputStream)
69 br = (BufferedInputStream) str;
70 else
71 br = new BufferedInputStream (str, BUFFER_INCREMENT);
72 next ();
73 if (index > 0)
74 move (index);
77 private boolean next ()
79 if (end == 1)
80 return false;
81 end--; // closer to end
83 try
85 if (index != -1)
87 br.reset ();
89 int i = br.read ();
90 br.mark (bufsize);
91 if (i == -1)
93 end = 1;
94 cached = OUT_OF_BOUNDS;
95 return false;
97 cached = (char) i;
98 index = 1;
99 } catch (IOException e)
101 e.printStackTrace ();
102 cached = OUT_OF_BOUNDS;
103 return false;
105 return true;
108 public char charAt (int index)
110 if (index == 0)
112 return cached;
114 else if (index >= end)
116 return OUT_OF_BOUNDS;
118 else if (index == -1)
120 return lookBehind[0];
122 else if (index == -2)
124 return lookBehind[1];
126 else if (index < -2)
128 return OUT_OF_BOUNDS;
130 else if (index >= bufsize)
132 // Allocate more space in the buffer.
135 while (bufsize <= index)
136 bufsize += BUFFER_INCREMENT;
137 br.reset ();
138 br.mark (bufsize);
139 br.skip (index - 1);
141 catch (IOException e)
145 else if (this.index != index)
149 br.reset ();
150 br.skip (index - 1);
152 catch (IOException e)
156 char ch = OUT_OF_BOUNDS;
160 int i = br.read ();
161 this.index = index + 1; // this.index is index of next pos relative to charAt(0)
162 if (i == -1)
164 // set flag that next should fail next time?
165 end = index;
166 return ch;
168 ch = (char) i;
169 } catch (IOException ie)
173 return ch;
176 public boolean move (int index)
178 // move read position [index] clicks from 'charAt(0)'
179 boolean retval = true;
180 while (retval && (index-- > 0))
181 retval = next ();
182 return retval;
185 public boolean isValid ()
187 return (cached != OUT_OF_BOUNDS);
190 public CharIndexed lookBehind (int index, int length)
192 throw new
193 UnsupportedOperationException
194 ("difficult to look behind for an input stream");
197 public int length ()
199 throw new
200 UnsupportedOperationException
201 ("difficult to tell the length for an input stream");
204 public void setLastMatch (REMatch match)
206 throw new
207 UnsupportedOperationException
208 ("difficult to support setLastMatch for an input stream");
211 public REMatch getLastMatch ()
213 throw new
214 UnsupportedOperationException
215 ("difficult to support getLastMatch for an input stream");
218 public void setHitEnd (REMatch match)
220 throw new
221 UnsupportedOperationException
222 ("difficult to support setHitEnd for an input stream");
225 public boolean hitEnd ()
227 throw new
228 UnsupportedOperationException
229 ("difficult to support hitEnd for an input stream");
232 public int getAnchor ()
234 throw new
235 UnsupportedOperationException
236 ("difficult to support getAnchor for an input stream");
239 public void setAnchor (int anchor)
241 throw new
242 UnsupportedOperationException
243 ("difficult to support setAnchor for an input stream");
246 public boolean move1 (int index)
248 throw new
249 UnsupportedOperationException
250 ("difficult to support move1 for an input stream");