1 /* gnu/regexp/CharIndexedInputStream.java 2 Copyright (C) 1998-2001, 2004, 2006 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 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. 10 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. 15 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. 20 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. 25 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. */ 37 38 package gnu.java.util.regex; 39 import java.io.BufferedInputStream; 40 import java.io.IOException; 41 import java.io.InputStream; 42 43 // TODO: move(x) shouldn't rely on calling next() x times 44 45 class CharIndexedInputStream implements CharIndexed 46 { 47 private static final int BUFFER_INCREMENT = 1024; 48 private static final int UNKNOWN = Integer.MAX_VALUE; // value for end 49 50 private BufferedInputStream br; 51 52 // so that we don't try to reset() right away 53 private int index = -1; 54 55 private int bufsize = BUFFER_INCREMENT; 56 57 private int end = UNKNOWN; 58 59 private char cached = OUT_OF_BOUNDS; 60 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 }; 65 CharIndexedInputStream(InputStream str, int index)66 CharIndexedInputStream (InputStream str, int index) 67 { 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); 75 } 76 next()77 private boolean next () 78 { 79 if (end == 1) 80 return false; 81 end--; // closer to end 82 83 try 84 { 85 if (index != -1) 86 { 87 br.reset (); 88 } 89 int i = br.read (); 90 br.mark (bufsize); 91 if (i == -1) 92 { 93 end = 1; 94 cached = OUT_OF_BOUNDS; 95 return false; 96 } 97 cached = (char) i; 98 index = 1; 99 } catch (IOException e) 100 { 101 e.printStackTrace (); 102 cached = OUT_OF_BOUNDS; 103 return false; 104 } 105 return true; 106 } 107 charAt(int index)108 public char charAt (int index) 109 { 110 if (index == 0) 111 { 112 return cached; 113 } 114 else if (index >= end) 115 { 116 return OUT_OF_BOUNDS; 117 } 118 else if (index == -1) 119 { 120 return lookBehind[0]; 121 } 122 else if (index == -2) 123 { 124 return lookBehind[1]; 125 } 126 else if (index < -2) 127 { 128 return OUT_OF_BOUNDS; 129 } 130 else if (index >= bufsize) 131 { 132 // Allocate more space in the buffer. 133 try 134 { 135 while (bufsize <= index) 136 bufsize += BUFFER_INCREMENT; 137 br.reset (); 138 br.mark (bufsize); 139 br.skip (index - 1); 140 } 141 catch (IOException e) 142 { 143 } 144 } 145 else if (this.index != index) 146 { 147 try 148 { 149 br.reset (); 150 br.skip (index - 1); 151 } 152 catch (IOException e) 153 { 154 } 155 } 156 char ch = OUT_OF_BOUNDS; 157 158 try 159 { 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) 163 { 164 // set flag that next should fail next time? 165 end = index; 166 return ch; 167 } 168 ch = (char) i; 169 } catch (IOException ie) 170 { 171 } 172 173 return ch; 174 } 175 move(int index)176 public boolean move (int index) 177 { 178 // move read position [index] clicks from 'charAt(0)' 179 boolean retval = true; 180 while (retval && (index-- > 0)) 181 retval = next (); 182 return retval; 183 } 184 isValid()185 public boolean isValid () 186 { 187 return (cached != OUT_OF_BOUNDS); 188 } 189 lookBehind(int index, int length)190 public CharIndexed lookBehind (int index, int length) 191 { 192 throw new 193 UnsupportedOperationException 194 ("difficult to look behind for an input stream"); 195 } 196 length()197 public int length () 198 { 199 throw new 200 UnsupportedOperationException 201 ("difficult to tell the length for an input stream"); 202 } 203 setLastMatch(REMatch match)204 public void setLastMatch (REMatch match) 205 { 206 throw new 207 UnsupportedOperationException 208 ("difficult to support setLastMatch for an input stream"); 209 } 210 getLastMatch()211 public REMatch getLastMatch () 212 { 213 throw new 214 UnsupportedOperationException 215 ("difficult to support getLastMatch for an input stream"); 216 } 217 setHitEnd(REMatch match)218 public void setHitEnd (REMatch match) 219 { 220 throw new 221 UnsupportedOperationException 222 ("difficult to support setHitEnd for an input stream"); 223 } 224 hitEnd()225 public boolean hitEnd () 226 { 227 throw new 228 UnsupportedOperationException 229 ("difficult to support hitEnd for an input stream"); 230 } 231 getAnchor()232 public int getAnchor () 233 { 234 throw new 235 UnsupportedOperationException 236 ("difficult to support getAnchor for an input stream"); 237 } 238 setAnchor(int anchor)239 public void setAnchor (int anchor) 240 { 241 throw new 242 UnsupportedOperationException 243 ("difficult to support setAnchor for an input stream"); 244 } 245 move1(int index)246 public boolean move1 (int index) 247 { 248 throw new 249 UnsupportedOperationException 250 ("difficult to support move1 for an input stream"); 251 } 252 253 } 254