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. */
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
{
46 private static final int BUFFER_INCREMENT
= 1024;
47 private static final int UNKNOWN
= Integer
.MAX_VALUE
; // value for end
49 private BufferedInputStream br
;
51 // so that we don't try to reset() right away
52 private int index
= -1;
54 private int bufsize
= BUFFER_INCREMENT
;
56 private int end
= UNKNOWN
;
58 private char cached
= OUT_OF_BOUNDS
;
60 // Big enough for a \r\n pair
61 // lookBehind[0] = most recent
62 // lookBehind[1] = second most recent
63 private char[] lookBehind
= new char[] { OUT_OF_BOUNDS
, OUT_OF_BOUNDS
};
65 CharIndexedInputStream(InputStream str
, int index
) {
66 if (str
instanceof BufferedInputStream
) br
= (BufferedInputStream
) str
;
67 else br
= new BufferedInputStream(str
,BUFFER_INCREMENT
);
69 if (index
> 0) move(index
);
72 private boolean next() {
73 if (end
== 1) return false;
74 end
--; // closer to end
84 cached
= OUT_OF_BOUNDS
;
89 } catch (IOException e
) {
91 cached
= OUT_OF_BOUNDS
;
97 public char charAt(int index
) {
100 } else if (index
>= end
) {
101 return OUT_OF_BOUNDS
;
102 } else if (index
== -1) {
103 return lookBehind
[0];
104 } else if (index
== -2) {
105 return lookBehind
[1];
106 } else if (index
< -2) {
107 return OUT_OF_BOUNDS
;
108 } else if (index
>= bufsize
) {
109 // Allocate more space in the buffer.
111 while (bufsize
<= index
) bufsize
+= BUFFER_INCREMENT
;
115 } catch (IOException e
) { }
116 } else if (this.index
!= index
) {
120 } catch (IOException e
) { }
122 char ch
= OUT_OF_BOUNDS
;
126 this.index
= index
+1; // this.index is index of next pos relative to charAt(0)
128 // set flag that next should fail next time?
133 } catch (IOException ie
) { }
138 public boolean move(int index
) {
139 // move read position [index] clicks from 'charAt(0)'
140 boolean retval
= true;
141 while (retval
&& (index
-- > 0)) retval
= next();
145 public boolean isValid() {
146 return (cached
!= OUT_OF_BOUNDS
);
149 public CharIndexed
lookBehind(int index
, int length
) {
150 throw new UnsupportedOperationException(
151 "difficult to look behind for an input stream");
154 public int length() {
155 throw new UnsupportedOperationException(
156 "difficult to tell the length for an input stream");