001/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 5.0 */ 002/* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */ 003/**************************************************************** 004 * Licensed to the Apache Software Foundation (ASF) under one * 005 * or more contributor license agreements. See the NOTICE file * 006 * distributed with this work for additional information * 007 * regarding copyright ownership. The ASF licenses this file * 008 * to you under the Apache License, Version 2.0 (the * 009 * "License"); you may not use this file except in compliance * 010 * with the License. You may obtain a copy of the License at * 011 * * 012 * http://www.apache.org/licenses/LICENSE-2.0 * 013 * * 014 * Unless required by applicable law or agreed to in writing, * 015 * software distributed under the License is distributed on an * 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 017 * KIND, either express or implied. See the License for the * 018 * specific language governing permissions and limitations * 019 * under the License. * 020 ****************************************************************/ 021package org.apache.james.mime4j.field.address; 022 023/** 024 * An implementation of interface CharStream, where the stream is assumed to 025 * contain only ASCII characters (without unicode processing). 026 */ 027 028public class SimpleCharStream 029{ 030/** Whether parser is static. */ 031 public static final boolean staticFlag = false; 032 int bufsize; 033 int available; 034 int tokenBegin; 035/** Position in buffer. */ 036 public int bufpos = -1; 037 protected int bufline[]; 038 protected int bufcolumn[]; 039 040 protected int column = 0; 041 protected int line = 1; 042 043 protected boolean prevCharIsCR = false; 044 protected boolean prevCharIsLF = false; 045 046 protected java.io.Reader inputStream; 047 048 protected char[] buffer; 049 protected int maxNextCharInd = 0; 050 protected int inBuf = 0; 051 protected int tabSize = 8; 052 053 protected void setTabSize(int i) { tabSize = i; } 054 protected int getTabSize(int i) { return tabSize; } 055 056 057 protected void ExpandBuff(boolean wrapAround) 058 { 059 char[] newbuffer = new char[bufsize + 2048]; 060 int newbufline[] = new int[bufsize + 2048]; 061 int newbufcolumn[] = new int[bufsize + 2048]; 062 063 try 064 { 065 if (wrapAround) 066 { 067 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 068 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos); 069 buffer = newbuffer; 070 071 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 072 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 073 bufline = newbufline; 074 075 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 076 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); 077 bufcolumn = newbufcolumn; 078 079 maxNextCharInd = (bufpos += (bufsize - tokenBegin)); 080 } 081 else 082 { 083 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 084 buffer = newbuffer; 085 086 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 087 bufline = newbufline; 088 089 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 090 bufcolumn = newbufcolumn; 091 092 maxNextCharInd = (bufpos -= tokenBegin); 093 } 094 } 095 catch (Throwable t) 096 { 097 throw new Error(t.getMessage()); 098 } 099 100 101 bufsize += 2048; 102 available = bufsize; 103 tokenBegin = 0; 104 } 105 106 protected void FillBuff() throws java.io.IOException 107 { 108 if (maxNextCharInd == available) 109 { 110 if (available == bufsize) 111 { 112 if (tokenBegin > 2048) 113 { 114 bufpos = maxNextCharInd = 0; 115 available = tokenBegin; 116 } 117 else if (tokenBegin < 0) 118 bufpos = maxNextCharInd = 0; 119 else 120 ExpandBuff(false); 121 } 122 else if (available > tokenBegin) 123 available = bufsize; 124 else if ((tokenBegin - available) < 2048) 125 ExpandBuff(true); 126 else 127 available = tokenBegin; 128 } 129 130 int i; 131 try { 132 if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1) 133 { 134 inputStream.close(); 135 throw new java.io.IOException(); 136 } 137 else 138 maxNextCharInd += i; 139 return; 140 } 141 catch(java.io.IOException e) { 142 --bufpos; 143 backup(0); 144 if (tokenBegin == -1) 145 tokenBegin = bufpos; 146 throw e; 147 } 148 } 149 150/** Start. */ 151 public char BeginToken() throws java.io.IOException 152 { 153 tokenBegin = -1; 154 char c = readChar(); 155 tokenBegin = bufpos; 156 157 return c; 158 } 159 160 protected void UpdateLineColumn(char c) 161 { 162 column++; 163 164 if (prevCharIsLF) 165 { 166 prevCharIsLF = false; 167 line += (column = 1); 168 } 169 else if (prevCharIsCR) 170 { 171 prevCharIsCR = false; 172 if (c == '\n') 173 { 174 prevCharIsLF = true; 175 } 176 else 177 line += (column = 1); 178 } 179 180 switch (c) 181 { 182 case '\r' : 183 prevCharIsCR = true; 184 break; 185 case '\n' : 186 prevCharIsLF = true; 187 break; 188 case '\t' : 189 column--; 190 column += (tabSize - (column % tabSize)); 191 break; 192 default : 193 break; 194 } 195 196 bufline[bufpos] = line; 197 bufcolumn[bufpos] = column; 198 } 199 200/** Read a character. */ 201 public char readChar() throws java.io.IOException 202 { 203 if (inBuf > 0) 204 { 205 --inBuf; 206 207 if (++bufpos == bufsize) 208 bufpos = 0; 209 210 return buffer[bufpos]; 211 } 212 213 if (++bufpos >= maxNextCharInd) 214 FillBuff(); 215 216 char c = buffer[bufpos]; 217 218 UpdateLineColumn(c); 219 return c; 220 } 221 222 @Deprecated 223 /** 224 * @deprecated 225 * @see #getEndColumn 226 */ 227 228 public int getColumn() { 229 return bufcolumn[bufpos]; 230 } 231 232 @Deprecated 233 /** 234 * @deprecated 235 * @see #getEndLine 236 */ 237 238 public int getLine() { 239 return bufline[bufpos]; 240 } 241 242 /** Get token end column number. */ 243 public int getEndColumn() { 244 return bufcolumn[bufpos]; 245 } 246 247 /** Get token end line number. */ 248 public int getEndLine() { 249 return bufline[bufpos]; 250 } 251 252 /** Get token beginning column number. */ 253 public int getBeginColumn() { 254 return bufcolumn[tokenBegin]; 255 } 256 257 /** Get token beginning line number. */ 258 public int getBeginLine() { 259 return bufline[tokenBegin]; 260 } 261 262/** Backup a number of characters. */ 263 public void backup(int amount) { 264 265 inBuf += amount; 266 if ((bufpos -= amount) < 0) 267 bufpos += bufsize; 268 } 269 270 /** Constructor. */ 271 public SimpleCharStream(java.io.Reader dstream, int startline, 272 int startcolumn, int buffersize) 273 { 274 inputStream = dstream; 275 line = startline; 276 column = startcolumn - 1; 277 278 available = bufsize = buffersize; 279 buffer = new char[buffersize]; 280 bufline = new int[buffersize]; 281 bufcolumn = new int[buffersize]; 282 } 283 284 /** Constructor. */ 285 public SimpleCharStream(java.io.Reader dstream, int startline, 286 int startcolumn) 287 { 288 this(dstream, startline, startcolumn, 4096); 289 } 290 291 /** Constructor. */ 292 public SimpleCharStream(java.io.Reader dstream) 293 { 294 this(dstream, 1, 1, 4096); 295 } 296 297 /** Reinitialise. */ 298 public void ReInit(java.io.Reader dstream, int startline, 299 int startcolumn, int buffersize) 300 { 301 inputStream = dstream; 302 line = startline; 303 column = startcolumn - 1; 304 305 if (buffer == null || buffersize != buffer.length) 306 { 307 available = bufsize = buffersize; 308 buffer = new char[buffersize]; 309 bufline = new int[buffersize]; 310 bufcolumn = new int[buffersize]; 311 } 312 prevCharIsLF = prevCharIsCR = false; 313 tokenBegin = inBuf = maxNextCharInd = 0; 314 bufpos = -1; 315 } 316 317 /** Reinitialise. */ 318 public void ReInit(java.io.Reader dstream, int startline, 319 int startcolumn) 320 { 321 ReInit(dstream, startline, startcolumn, 4096); 322 } 323 324 /** Reinitialise. */ 325 public void ReInit(java.io.Reader dstream) 326 { 327 ReInit(dstream, 1, 1, 4096); 328 } 329 /** Constructor. */ 330 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, 331 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException 332 { 333 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); 334 } 335 336 /** Constructor. */ 337 public SimpleCharStream(java.io.InputStream dstream, int startline, 338 int startcolumn, int buffersize) 339 { 340 this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); 341 } 342 343 /** Constructor. */ 344 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, 345 int startcolumn) throws java.io.UnsupportedEncodingException 346 { 347 this(dstream, encoding, startline, startcolumn, 4096); 348 } 349 350 /** Constructor. */ 351 public SimpleCharStream(java.io.InputStream dstream, int startline, 352 int startcolumn) 353 { 354 this(dstream, startline, startcolumn, 4096); 355 } 356 357 /** Constructor. */ 358 public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException 359 { 360 this(dstream, encoding, 1, 1, 4096); 361 } 362 363 /** Constructor. */ 364 public SimpleCharStream(java.io.InputStream dstream) 365 { 366 this(dstream, 1, 1, 4096); 367 } 368 369 /** Reinitialise. */ 370 public void ReInit(java.io.InputStream dstream, String encoding, int startline, 371 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException 372 { 373 ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); 374 } 375 376 /** Reinitialise. */ 377 public void ReInit(java.io.InputStream dstream, int startline, 378 int startcolumn, int buffersize) 379 { 380 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); 381 } 382 383 /** Reinitialise. */ 384 public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException 385 { 386 ReInit(dstream, encoding, 1, 1, 4096); 387 } 388 389 /** Reinitialise. */ 390 public void ReInit(java.io.InputStream dstream) 391 { 392 ReInit(dstream, 1, 1, 4096); 393 } 394 /** Reinitialise. */ 395 public void ReInit(java.io.InputStream dstream, String encoding, int startline, 396 int startcolumn) throws java.io.UnsupportedEncodingException 397 { 398 ReInit(dstream, encoding, startline, startcolumn, 4096); 399 } 400 /** Reinitialise. */ 401 public void ReInit(java.io.InputStream dstream, int startline, 402 int startcolumn) 403 { 404 ReInit(dstream, startline, startcolumn, 4096); 405 } 406 /** Get token literal value. */ 407 public String GetImage() 408 { 409 if (bufpos >= tokenBegin) 410 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); 411 else 412 return new String(buffer, tokenBegin, bufsize - tokenBegin) + 413 new String(buffer, 0, bufpos + 1); 414 } 415 416 /** Get the suffix. */ 417 public char[] GetSuffix(int len) 418 { 419 char[] ret = new char[len]; 420 421 if ((bufpos + 1) >= len) 422 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 423 else 424 { 425 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, 426 len - bufpos - 1); 427 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 428 } 429 430 return ret; 431 } 432 433 /** Reset buffer when finished. */ 434 public void Done() 435 { 436 buffer = null; 437 bufline = null; 438 bufcolumn = null; 439 } 440 441 /** 442 * Method to adjust line and column numbers for the start of a token. 443 */ 444 public void adjustBeginLineColumn(int newLine, int newCol) 445 { 446 int start = tokenBegin; 447 int len; 448 449 if (bufpos >= tokenBegin) 450 { 451 len = bufpos - tokenBegin + inBuf + 1; 452 } 453 else 454 { 455 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 456 } 457 458 int i = 0, j = 0, k = 0; 459 int nextColDiff = 0, columnDiff = 0; 460 461 while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) 462 { 463 bufline[j] = newLine; 464 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; 465 bufcolumn[j] = newCol + columnDiff; 466 columnDiff = nextColDiff; 467 i++; 468 } 469 470 if (i < len) 471 { 472 bufline[j] = newLine++; 473 bufcolumn[j] = newCol + columnDiff; 474 475 while (i++ < len) 476 { 477 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) 478 bufline[j] = newLine++; 479 else 480 bufline[j] = newLine; 481 } 482 } 483 484 line = bufline[j]; 485 column = bufcolumn[j]; 486 } 487 488} 489/* JavaCC - OriginalChecksum=1470b6c00a5eeb0e437d692b18156fc1 (do not edit this line) */