1 /*
   2  * Copyright (c) 1997, 2004, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /* Generated By:JavaCC: Do not edit this line. ASCII_CharStream.java Version 0.7pre6 */
  27 package com.sun.jmx.snmp.IPAcl;
  28 
  29 /**
  30  * An implementation of interface CharStream, where the stream is assumed to
  31  * contain only ASCII characters (without unicode processing).
  32  */
  33 
  34 final class ASCII_CharStream
  35 {
  36   public static final boolean staticFlag = false;
  37   int bufsize;
  38   int available;
  39   int tokenBegin;
  40   public int bufpos = -1;
  41   private int bufline[];
  42   private int bufcolumn[];
  43 
  44   private int column = 0;
  45   private int line = 1;
  46 
  47   private boolean prevCharIsCR = false;
  48   private boolean prevCharIsLF = false;
  49 
  50   private java.io.Reader inputStream;
  51 
  52   private char[] buffer;
  53   private int maxNextCharInd = 0;
  54   private int inBuf = 0;
  55 
  56   private final void ExpandBuff(boolean wrapAround)
  57   {
  58      char[] newbuffer = new char[bufsize + 2048];
  59      int newbufline[] = new int[bufsize + 2048];
  60      int newbufcolumn[] = new int[bufsize + 2048];
  61 
  62      try
  63      {
  64         if (wrapAround)
  65         {
  66            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  67            System.arraycopy(buffer, 0, newbuffer,
  68                                              bufsize - tokenBegin, bufpos);
  69            buffer = newbuffer;
  70 
  71            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  72            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
  73            bufline = newbufline;
  74 
  75            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  76            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
  77            bufcolumn = newbufcolumn;
  78 
  79            maxNextCharInd = (bufpos += (bufsize - tokenBegin));
  80         }
  81         else
  82         {
  83            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  84            buffer = newbuffer;
  85 
  86            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  87            bufline = newbufline;
  88 
  89            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  90            bufcolumn = newbufcolumn;
  91 
  92            maxNextCharInd = (bufpos -= tokenBegin);
  93         }
  94      }
  95      catch (Throwable t)
  96      {
  97         throw new Error(t.getMessage());
  98      }
  99 
 100 
 101      bufsize += 2048;
 102      available = bufsize;
 103      tokenBegin = 0;
 104   }
 105 
 106   private final 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,
 133                                     available - maxNextCharInd)) == -1)
 134         {
 135            inputStream.close();
 136            throw new java.io.IOException();
 137         }
 138         else
 139            maxNextCharInd += i;
 140         return;
 141      }
 142      catch(java.io.IOException e) {
 143         --bufpos;
 144         backup(0);
 145         if (tokenBegin == -1)
 146            tokenBegin = bufpos;
 147         throw e;
 148      }
 149   }
 150 
 151   public final char BeginToken() throws java.io.IOException
 152   {
 153      tokenBegin = -1;
 154      char c = readChar();
 155      tokenBegin = bufpos;
 156 
 157      return c;
 158   }
 159 
 160   private final 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 += (8 - (column & 07));
 191            break;
 192         default :
 193            break;
 194      }
 195 
 196      bufline[bufpos] = line;
 197      bufcolumn[bufpos] = column;
 198   }
 199 
 200   public final char readChar() throws java.io.IOException
 201   {
 202      if (inBuf > 0)
 203      {
 204         --inBuf;
 205         return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]);
 206      }
 207 
 208      if (++bufpos >= maxNextCharInd)
 209         FillBuff();
 210 
 211      char c = (char)((char)0xff & buffer[bufpos]);
 212 
 213      UpdateLineColumn(c);
 214      return (c);
 215   }
 216 
 217   /**
 218    * @deprecated
 219    * @see #getEndColumn
 220    */
 221     @Deprecated
 222   public final int getColumn() {
 223      return bufcolumn[bufpos];
 224   }
 225 
 226   /**
 227    * @deprecated
 228    * @see #getEndLine
 229    */
 230     @Deprecated
 231   public final int getLine() {
 232      return bufline[bufpos];
 233   }
 234 
 235   public final int getEndColumn() {
 236      return bufcolumn[bufpos];
 237   }
 238 
 239   public final int getEndLine() {
 240      return bufline[bufpos];
 241   }
 242 
 243   public final int getBeginColumn() {
 244      return bufcolumn[tokenBegin];
 245   }
 246 
 247   public final int getBeginLine() {
 248      return bufline[tokenBegin];
 249   }
 250 
 251   public final void backup(int amount) {
 252 
 253     inBuf += amount;
 254     if ((bufpos -= amount) < 0)
 255        bufpos += bufsize;
 256   }
 257 
 258   public ASCII_CharStream(java.io.Reader dstream, int startline,
 259   int startcolumn, int buffersize)
 260   {
 261     inputStream = dstream;
 262     line = startline;
 263     column = startcolumn - 1;
 264 
 265     available = bufsize = buffersize;
 266     buffer = new char[buffersize];
 267     bufline = new int[buffersize];
 268     bufcolumn = new int[buffersize];
 269   }
 270 
 271   public ASCII_CharStream(java.io.Reader dstream, int startline,
 272                                                            int startcolumn)
 273   {
 274      this(dstream, startline, startcolumn, 4096);
 275   }
 276   public void ReInit(java.io.Reader dstream, int startline,
 277   int startcolumn, int buffersize)
 278   {
 279     inputStream = dstream;
 280     line = startline;
 281     column = startcolumn - 1;
 282 
 283     if (buffer == null || buffersize != buffer.length)
 284     {
 285       available = bufsize = buffersize;
 286       buffer = new char[buffersize];
 287       bufline = new int[buffersize];
 288       bufcolumn = new int[buffersize];
 289     }
 290     prevCharIsLF = prevCharIsCR = false;
 291     tokenBegin = inBuf = maxNextCharInd = 0;
 292     bufpos = -1;
 293   }
 294 
 295   public void ReInit(java.io.Reader dstream, int startline,
 296                                                            int startcolumn)
 297   {
 298      ReInit(dstream, startline, startcolumn, 4096);
 299   }
 300   public ASCII_CharStream(java.io.InputStream dstream, int startline,
 301   int startcolumn, int buffersize)
 302   {
 303      this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
 304   }
 305 
 306   public ASCII_CharStream(java.io.InputStream dstream, int startline,
 307                                                            int startcolumn)
 308   {
 309      this(dstream, startline, startcolumn, 4096);
 310   }
 311 
 312   public void ReInit(java.io.InputStream dstream, int startline,
 313   int startcolumn, int buffersize)
 314   {
 315      ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
 316   }
 317   public void ReInit(java.io.InputStream dstream, int startline,
 318                                                            int startcolumn)
 319   {
 320      ReInit(dstream, startline, startcolumn, 4096);
 321   }
 322   public final String GetImage()
 323   {
 324      if (bufpos >= tokenBegin)
 325         return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
 326      else
 327         return new String(buffer, tokenBegin, bufsize - tokenBegin) +
 328                               new String(buffer, 0, bufpos + 1);
 329   }
 330 
 331   public final char[] GetSuffix(int len)
 332   {
 333      char[] ret = new char[len];
 334 
 335      if ((bufpos + 1) >= len)
 336         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
 337      else
 338      {
 339         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
 340                                                           len - bufpos - 1);
 341         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
 342      }
 343 
 344      return ret;
 345   }
 346 
 347   public void Done()
 348   {
 349      buffer = null;
 350      bufline = null;
 351      bufcolumn = null;
 352   }
 353 
 354   /**
 355    * Method to adjust line and column numbers for the start of a token.
 356    */
 357   public void adjustBeginLineColumn(int newLine, int newCol)
 358   {
 359      int start = tokenBegin;
 360      int len;
 361 
 362      if (bufpos >= tokenBegin)
 363      {
 364         len = bufpos - tokenBegin + inBuf + 1;
 365      }
 366      else
 367      {
 368         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
 369      }
 370 
 371      int i = 0, j = 0, k = 0;
 372      int nextColDiff = 0, columnDiff = 0;
 373 
 374      while (i < len &&
 375             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
 376      {
 377         bufline[j] = newLine;
 378         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
 379         bufcolumn[j] = newCol + columnDiff;
 380         columnDiff = nextColDiff;
 381         i++;
 382      }
 383 
 384      if (i < len)
 385      {
 386         bufline[j] = newLine++;
 387         bufcolumn[j] = newCol + columnDiff;
 388 
 389         while (i++ < len)
 390         {
 391            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
 392               bufline[j] = newLine++;
 393            else
 394               bufline[j] = newLine;
 395         }
 396      }
 397 
 398      line = bufline[j];
 399      column = bufcolumn[j];
 400   }
 401 
 402 }