1 19 20 21 package org.netbeans.modules.debugger.jpda.expr; 22 23 27 28 class SimpleCharStream 29 { 30 public static final boolean staticFlag = false; 31 int bufsize; 32 int available; 33 int tokenBegin; 34 public int bufpos = -1; 35 protected int bufline[]; 36 protected int bufcolumn[]; 37 38 protected int column = 0; 39 protected int line = 1; 40 41 protected boolean prevCharIsCR = false; 42 protected boolean prevCharIsLF = false; 43 44 protected java.io.Reader inputStream; 45 46 protected char[] buffer; 47 protected int maxNextCharInd = 0; 48 protected int inBuf = 0; 49 50 protected void ExpandBuff(boolean wrapAround) 51 { 52 char[] newbuffer = new char[bufsize + 2048]; 53 int newbufline[] = new int[bufsize + 2048]; 54 int newbufcolumn[] = new int[bufsize + 2048]; 55 56 if (wrapAround) 57 { 58 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 59 System.arraycopy(buffer, 0, newbuffer, 60 bufsize - tokenBegin, bufpos); 61 buffer = newbuffer; 62 63 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 64 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 65 bufline = newbufline; 66 67 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 68 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); 69 bufcolumn = newbufcolumn; 70 71 maxNextCharInd = (bufpos += (bufsize - tokenBegin)); 72 } 73 else 74 { 75 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 76 buffer = newbuffer; 77 78 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 79 bufline = newbufline; 80 81 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 82 bufcolumn = newbufcolumn; 83 84 maxNextCharInd = (bufpos -= tokenBegin); 85 } 86 87 88 bufsize += 2048; 89 available = bufsize; 90 tokenBegin = 0; 91 } 92 93 protected void FillBuff() throws java.io.IOException 94 { 95 if (maxNextCharInd == available) 96 { 97 if (available == bufsize) 98 { 99 if (tokenBegin > 2048) 100 { 101 bufpos = maxNextCharInd = 0; 102 available = tokenBegin; 103 } 104 else if (tokenBegin < 0) 105 bufpos = maxNextCharInd = 0; 106 else 107 ExpandBuff(false); 108 } 109 else if (available > tokenBegin) 110 available = bufsize; 111 else if ((tokenBegin - available) < 2048) 112 ExpandBuff(true); 113 else 114 available = tokenBegin; 115 } 116 117 int i; 118 try { 119 if ((i = inputStream.read(buffer, maxNextCharInd, 120 available - maxNextCharInd)) == -1) 121 { 122 inputStream.close(); 123 throw new java.io.IOException (); 124 } 125 else 126 maxNextCharInd += i; 127 return; 128 } 129 catch(java.io.IOException e) { 130 --bufpos; 131 backup(0); 132 if (tokenBegin == -1) 133 tokenBegin = bufpos; 134 throw e; 135 } 136 } 137 138 public char BeginToken() throws java.io.IOException 139 { 140 tokenBegin = -1; 141 char c = readChar(); 142 tokenBegin = bufpos; 143 144 return c; 145 } 146 147 protected void UpdateLineColumn(char c) 148 { 149 column++; 150 151 if (prevCharIsLF) 152 { 153 prevCharIsLF = false; 154 line += (column = 1); 155 } 156 else if (prevCharIsCR) 157 { 158 prevCharIsCR = false; 159 if (c == '\n') 160 { 161 prevCharIsLF = true; 162 } 163 else 164 line += (column = 1); 165 } 166 167 switch (c) 168 { 169 case '\r' : 170 prevCharIsCR = true; 171 break; 172 case '\n' : 173 prevCharIsLF = true; 174 break; 175 case '\t' : 176 column--; 177 column += (8 - (column & 07)); 178 break; 179 default : 180 break; 181 } 182 183 bufline[bufpos] = line; 184 bufcolumn[bufpos] = column; 185 } 186 187 public char readChar() throws java.io.IOException 188 { 189 if (inBuf > 0) 190 { 191 --inBuf; 192 193 if (++bufpos == bufsize) 194 bufpos = 0; 195 196 return buffer[bufpos]; 197 } 198 199 if (++bufpos >= maxNextCharInd) 200 FillBuff(); 201 202 char c = buffer[bufpos]; 203 204 UpdateLineColumn(c); 205 return (c); 206 } 207 208 212 213 public int getColumn() { 214 return bufcolumn[bufpos]; 215 } 216 217 221 222 public int getLine() { 223 return bufline[bufpos]; 224 } 225 226 public int getEndColumn() { 227 return bufcolumn[bufpos]; 228 } 229 230 public int getEndLine() { 231 return bufline[bufpos]; 232 } 233 234 public int getBeginColumn() { 235 return bufcolumn[tokenBegin]; 236 } 237 238 public int getBeginLine() { 239 return bufline[tokenBegin]; 240 } 241 242 public void backup(int amount) { 243 244 inBuf += amount; 245 if ((bufpos -= amount) < 0) 246 bufpos += bufsize; 247 } 248 249 public SimpleCharStream(java.io.Reader dstream, int startline, 250 int startcolumn, int buffersize) 251 { 252 inputStream = dstream; 253 line = startline; 254 column = startcolumn - 1; 255 256 available = bufsize = buffersize; 257 buffer = new char[buffersize]; 258 bufline = new int[buffersize]; 259 bufcolumn = new int[buffersize]; 260 } 261 262 public SimpleCharStream(java.io.Reader dstream, int startline, 263 int startcolumn) 264 { 265 this(dstream, startline, startcolumn, 4096); 266 } 267 268 public SimpleCharStream(java.io.Reader dstream) 269 { 270 this(dstream, 1, 1, 4096); 271 } 272 public void ReInit(java.io.Reader dstream, int startline, 273 int startcolumn, int buffersize) 274 { 275 inputStream = dstream; 276 line = startline; 277 column = startcolumn - 1; 278 279 if (buffer == null || buffersize != buffer.length) 280 { 281 available = bufsize = buffersize; 282 buffer = new char[buffersize]; 283 bufline = new int[buffersize]; 284 bufcolumn = new int[buffersize]; 285 } 286 prevCharIsLF = prevCharIsCR = false; 287 tokenBegin = inBuf = maxNextCharInd = 0; 288 bufpos = -1; 289 } 290 291 public void ReInit(java.io.Reader dstream, int startline, 292 int startcolumn) 293 { 294 ReInit(dstream, startline, startcolumn, 4096); 295 } 296 297 public void ReInit(java.io.Reader dstream) 298 { 299 ReInit(dstream, 1, 1, 4096); 300 } 301 public SimpleCharStream(java.io.InputStream dstream, int startline, 302 int startcolumn, int buffersize) 303 { 304 this(new java.io.InputStreamReader (dstream), startline, startcolumn, 4096); 305 } 306 307 public SimpleCharStream(java.io.InputStream dstream, int startline, 308 int startcolumn) 309 { 310 this(dstream, startline, startcolumn, 4096); 311 } 312 313 public SimpleCharStream(java.io.InputStream dstream) 314 { 315 this(dstream, 1, 1, 4096); 316 } 317 318 public void ReInit(java.io.InputStream dstream, int startline, 319 int startcolumn, int buffersize) 320 { 321 ReInit(new java.io.InputStreamReader (dstream), startline, startcolumn, 4096); 322 } 323 324 public void ReInit(java.io.InputStream dstream) 325 { 326 ReInit(dstream, 1, 1, 4096); 327 } 328 public void ReInit(java.io.InputStream dstream, int startline, 329 int startcolumn) 330 { 331 ReInit(dstream, startline, startcolumn, 4096); 332 } 333 public String GetImage() 334 { 335 if (bufpos >= tokenBegin) 336 return new String (buffer, tokenBegin, bufpos - tokenBegin + 1); 337 else 338 return new String (buffer, tokenBegin, bufsize - tokenBegin) + 339 new String (buffer, 0, bufpos + 1); 340 } 341 342 public char[] GetSuffix(int len) 343 { 344 char[] ret = new char[len]; 345 346 if ((bufpos + 1) >= len) 347 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 348 else 349 { 350 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, 351 len - bufpos - 1); 352 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 353 } 354 355 return ret; 356 } 357 358 public void Done() 359 { 360 buffer = null; 361 bufline = null; 362 bufcolumn = null; 363 } 364 365 368 public void adjustBeginLineColumn(int newLine, int newCol) 369 { 370 int start = tokenBegin; 371 int len; 372 373 if (bufpos >= tokenBegin) 374 { 375 len = bufpos - tokenBegin + inBuf + 1; 376 } 377 else 378 { 379 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 380 } 381 382 int i = 0, j = 0, k = 0; 383 int nextColDiff = 0, columnDiff = 0; 384 385 while (i < len && 386 bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) 387 { 388 bufline[j] = newLine; 389 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; 390 bufcolumn[j] = newCol + columnDiff; 391 columnDiff = nextColDiff; 392 i++; 393 } 394 395 if (i < len) 396 { 397 bufline[j] = newLine++; 398 bufcolumn[j] = newCol + columnDiff; 399 400 while (i++ < len) 401 { 402 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) 403 bufline[j] = newLine++; 404 else 405 bufline[j] = newLine; 406 } 407 } 408 409 line = bufline[j]; 410 column = bufcolumn[j]; 411 } 412 413 } 414 | Popular Tags |