001/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 7.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 = 1;
052  protected boolean trackLineColumn = true;
053
054  public void setTabSize(int i) { tabSize = i; }
055  public int getTabSize() { return tabSize; }
056
057
058
059  protected void ExpandBuff(boolean wrapAround)
060  {
061    char[] newbuffer = new char[bufsize + 2048];
062    int newbufline[] = new int[bufsize + 2048];
063    int newbufcolumn[] = new int[bufsize + 2048];
064
065    try
066    {
067      if (wrapAround)
068      {
069        System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
070        System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
071        buffer = newbuffer;
072
073        System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
074        System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
075        bufline = newbufline;
076
077        System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
078        System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
079        bufcolumn = newbufcolumn;
080
081        maxNextCharInd = (bufpos += (bufsize - tokenBegin));
082      }
083      else
084      {
085        System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
086        buffer = newbuffer;
087
088        System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
089        bufline = newbufline;
090
091        System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
092        bufcolumn = newbufcolumn;
093
094        maxNextCharInd = (bufpos -= tokenBegin);
095      }
096    }
097    catch (Throwable t)
098    {
099      throw new Error(t.getMessage());
100    }
101
102
103    bufsize += 2048;
104    available = bufsize;
105    tokenBegin = 0;
106  }
107
108  protected void FillBuff() throws java.io.IOException
109  {
110    if (maxNextCharInd == available)
111    {
112      if (available == bufsize)
113      {
114        if (tokenBegin > 2048)
115        {
116          bufpos = maxNextCharInd = 0;
117          available = tokenBegin;
118        }
119        else if (tokenBegin < 0)
120          bufpos = maxNextCharInd = 0;
121        else
122          ExpandBuff(false);
123      }
124      else if (available > tokenBegin)
125        available = bufsize;
126      else if ((tokenBegin - available) < 2048)
127        ExpandBuff(true);
128      else
129        available = tokenBegin;
130    }
131
132    int i;
133    try {
134      if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1)
135      {
136        inputStream.close();
137        throw new java.io.IOException();
138      }
139      else
140        maxNextCharInd += i;
141      return;
142    }
143    catch(java.io.IOException e) {
144      --bufpos;
145      backup(0);
146      if (tokenBegin == -1)
147        tokenBegin = bufpos;
148      throw e;
149    }
150  }
151
152/** Start. */
153  public char BeginToken() throws java.io.IOException
154  {
155    tokenBegin = -1;
156    char c = readChar();
157    tokenBegin = bufpos;
158
159    return c;
160  }
161
162  protected void UpdateLineColumn(char c)
163  {
164    column++;
165
166    if (prevCharIsLF)
167    {
168      prevCharIsLF = false;
169      line += (column = 1);
170    }
171    else if (prevCharIsCR)
172    {
173      prevCharIsCR = false;
174      if (c == '\n')
175      {
176        prevCharIsLF = true;
177      }
178      else
179        line += (column = 1);
180    }
181
182    switch (c)
183    {
184      case '\r' :
185        prevCharIsCR = true;
186        break;
187      case '\n' :
188        prevCharIsLF = true;
189        break;
190      case '\t' :
191        column--;
192        column += (tabSize - (column % tabSize));
193        break;
194      default :
195        break;
196    }
197
198    bufline[bufpos] = line;
199    bufcolumn[bufpos] = column;
200  }
201
202/** Read a character. */
203  public char readChar() throws java.io.IOException
204  {
205    if (inBuf > 0)
206    {
207      --inBuf;
208
209      if (++bufpos == bufsize)
210        bufpos = 0;
211
212      return buffer[bufpos];
213    }
214
215    if (++bufpos >= maxNextCharInd)
216      FillBuff();
217
218    char c = buffer[bufpos];
219
220    UpdateLineColumn(c);
221    return c;
222  }
223
224  @Deprecated
225  /**
226   * @deprecated
227   * @see #getEndColumn
228   */
229
230  public int getColumn() {
231    return bufcolumn[bufpos];
232  }
233
234  @Deprecated
235  /**
236   * @deprecated
237   * @see #getEndLine
238   */
239
240  public int getLine() {
241    return bufline[bufpos];
242  }
243
244  /** Get token end column number. */
245  public int getEndColumn() {
246    return bufcolumn[bufpos];
247  }
248
249  /** Get token end line number. */
250  public int getEndLine() {
251     return bufline[bufpos];
252  }
253
254  /** Get token beginning column number. */
255  public int getBeginColumn() {
256    return bufcolumn[tokenBegin];
257  }
258
259  /** Get token beginning line number. */
260  public int getBeginLine() {
261    return bufline[tokenBegin];
262  }
263
264/** Backup a number of characters. */
265  public void backup(int amount) {
266
267    inBuf += amount;
268    if ((bufpos -= amount) < 0)
269      bufpos += bufsize;
270  }
271
272  /** Constructor. */
273  public SimpleCharStream(java.io.Reader dstream, int startline,
274  int startcolumn, int buffersize)
275  {
276    inputStream = dstream;
277    line = startline;
278    column = startcolumn - 1;
279
280    available = bufsize = buffersize;
281    buffer = new char[buffersize];
282    bufline = new int[buffersize];
283    bufcolumn = new int[buffersize];
284  }
285
286  /** Constructor. */
287  public SimpleCharStream(java.io.Reader dstream, int startline,
288                          int startcolumn)
289  {
290    this(dstream, startline, startcolumn, 4096);
291  }
292
293  /** Constructor. */
294  public SimpleCharStream(java.io.Reader dstream)
295  {
296    this(dstream, 1, 1, 4096);
297  }
298
299  /** Reinitialise. */
300  public void ReInit(java.io.Reader dstream, int startline,
301  int startcolumn, int buffersize)
302  {
303    inputStream = dstream;
304    line = startline;
305    column = startcolumn - 1;
306
307    if (buffer == null || buffersize != buffer.length)
308    {
309      available = bufsize = buffersize;
310      buffer = new char[buffersize];
311      bufline = new int[buffersize];
312      bufcolumn = new int[buffersize];
313    }
314    prevCharIsLF = prevCharIsCR = false;
315    tokenBegin = inBuf = maxNextCharInd = 0;
316    bufpos = -1;
317  }
318
319  /** Reinitialise. */
320  public void ReInit(java.io.Reader dstream, int startline,
321                     int startcolumn)
322  {
323    ReInit(dstream, startline, startcolumn, 4096);
324  }
325
326  /** Reinitialise. */
327  public void ReInit(java.io.Reader dstream)
328  {
329    ReInit(dstream, 1, 1, 4096);
330  }
331  /** Constructor. */
332  public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
333  int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
334  {
335    this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
336  }
337
338  /** Constructor. */
339  public SimpleCharStream(java.io.InputStream dstream, int startline,
340  int startcolumn, int buffersize)
341  {
342    this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
343  }
344
345  /** Constructor. */
346  public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
347                          int startcolumn) throws java.io.UnsupportedEncodingException
348  {
349    this(dstream, encoding, startline, startcolumn, 4096);
350  }
351
352  /** Constructor. */
353  public SimpleCharStream(java.io.InputStream dstream, int startline,
354                          int startcolumn)
355  {
356    this(dstream, startline, startcolumn, 4096);
357  }
358
359  /** Constructor. */
360  public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
361  {
362    this(dstream, encoding, 1, 1, 4096);
363  }
364
365  /** Constructor. */
366  public SimpleCharStream(java.io.InputStream dstream)
367  {
368    this(dstream, 1, 1, 4096);
369  }
370
371  /** Reinitialise. */
372  public void ReInit(java.io.InputStream dstream, String encoding, int startline,
373                          int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
374  {
375    ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
376  }
377
378  /** Reinitialise. */
379  public void ReInit(java.io.InputStream dstream, int startline,
380                          int startcolumn, int buffersize)
381  {
382    ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
383  }
384
385  /** Reinitialise. */
386  public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
387  {
388    ReInit(dstream, encoding, 1, 1, 4096);
389  }
390
391  /** Reinitialise. */
392  public void ReInit(java.io.InputStream dstream)
393  {
394    ReInit(dstream, 1, 1, 4096);
395  }
396  /** Reinitialise. */
397  public void ReInit(java.io.InputStream dstream, String encoding, int startline,
398                     int startcolumn) throws java.io.UnsupportedEncodingException
399  {
400    ReInit(dstream, encoding, startline, startcolumn, 4096);
401  }
402  /** Reinitialise. */
403  public void ReInit(java.io.InputStream dstream, int startline,
404                     int startcolumn)
405  {
406    ReInit(dstream, startline, startcolumn, 4096);
407  }
408  /** Get token literal value. */
409  public String GetImage()
410  {
411    if (bufpos >= tokenBegin)
412      return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
413    else
414      return new String(buffer, tokenBegin, bufsize - tokenBegin) +
415                            new String(buffer, 0, bufpos + 1);
416  }
417
418  /** Get the suffix. */
419  public char[] GetSuffix(int len)
420  {
421    char[] ret = new char[len];
422
423    if ((bufpos + 1) >= len)
424      System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
425    else
426    {
427      System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
428                                                        len - bufpos - 1);
429      System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
430    }
431
432    return ret;
433  }
434
435  /** Reset buffer when finished. */
436  public void Done()
437  {
438    buffer = null;
439    bufline = null;
440    bufcolumn = null;
441  }
442
443  /**
444   * Method to adjust line and column numbers for the start of a token.
445   */
446  public void adjustBeginLineColumn(int newLine, int newCol)
447  {
448    int start = tokenBegin;
449    int len;
450
451    if (bufpos >= tokenBegin)
452    {
453      len = bufpos - tokenBegin + inBuf + 1;
454    }
455    else
456    {
457      len = bufsize - tokenBegin + bufpos + 1 + inBuf;
458    }
459
460    int i = 0, j = 0, k = 0;
461    int nextColDiff = 0, columnDiff = 0;
462
463    while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
464    {
465      bufline[j] = newLine;
466      nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
467      bufcolumn[j] = newCol + columnDiff;
468      columnDiff = nextColDiff;
469      i++;
470    }
471
472    if (i < len)
473    {
474      bufline[j] = newLine++;
475      bufcolumn[j] = newCol + columnDiff;
476
477      while (i++ < len)
478      {
479        if (bufline[j = start % bufsize] != bufline[++start % bufsize])
480          bufline[j] = newLine++;
481        else
482          bufline[j] = newLine;
483      }
484    }
485
486    line = bufline[j];
487    column = bufcolumn[j];
488  }
489  boolean getTrackLineColumn() { return trackLineColumn; }
490  void setTrackLineColumn(boolean tlc) { trackLineColumn = tlc; }
491}
492/* JavaCC - OriginalChecksum=61f13375d34a46805bc266c4f0330fca (do not edit this line) */