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)
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
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
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
;
71 br
= new BufferedInputStream (str
, BUFFER_INCREMENT
);
77 private boolean next ()
81 end
--; // closer to end
94 cached
= OUT_OF_BOUNDS
;
99 } catch (IOException e
)
101 e
.printStackTrace ();
102 cached
= OUT_OF_BOUNDS
;
108 public char charAt (int index
)
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];
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
;
141 catch (IOException e
)
145 else if (this.index
!= index
)
152 catch (IOException e
)
156 char ch
= OUT_OF_BOUNDS
;
161 this.index
= index
+ 1; // this.index is index of next pos relative to charAt(0)
164 // set flag that next should fail next time?
169 } catch (IOException ie
)
176 public boolean move (int index
)
178 // move read position [index] clicks from 'charAt(0)'
179 boolean retval
= true;
180 while (retval
&& (index
-- > 0))
185 public boolean isValid ()
187 return (cached
!= OUT_OF_BOUNDS
);
190 public CharIndexed
lookBehind (int index
, int length
)
193 UnsupportedOperationException
194 ("difficult to look behind for an input stream");
200 UnsupportedOperationException
201 ("difficult to tell the length for an input stream");
204 public void setLastMatch (REMatch match
)
207 UnsupportedOperationException
208 ("difficult to support setLastMatch for an input stream");
211 public REMatch
getLastMatch ()
214 UnsupportedOperationException
215 ("difficult to support getLastMatch for an input stream");
218 public void setHitEnd (REMatch match
)
221 UnsupportedOperationException
222 ("difficult to support setHitEnd for an input stream");
225 public boolean hitEnd ()
228 UnsupportedOperationException
229 ("difficult to support hitEnd for an input stream");
232 public int getAnchor ()
235 UnsupportedOperationException
236 ("difficult to support getAnchor for an input stream");
239 public void setAnchor (int anchor
)
242 UnsupportedOperationException
243 ("difficult to support setAnchor for an input stream");
246 public boolean move1 (int index
)
249 UnsupportedOperationException
250 ("difficult to support move1 for an input stream");