001    /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */
002    /**
003     * Licensed to the Apache Software Foundation (ASF) under one
004     * or more contributor license agreements.  See the NOTICE file
005     * distributed with this work for additional information
006     * regarding copyright ownership.  The ASF licenses this file
007     * to you under the Apache License, Version 2.0 (the
008     * "License"); you may not use this file except in compliance
009     * with the License.  You may obtain a copy of the License at
010     *
011     *     http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    package org.apache.hadoop.record.compiler.generated;
021    
022    import org.apache.hadoop.classification.InterfaceAudience;
023    import org.apache.hadoop.classification.InterfaceStability;
024    
025    /**
026     * An implementation of interface CharStream, where the stream is assumed to
027     * contain only ASCII characters (without unicode processing).
028     * 
029     * @deprecated Replaced by <a href="https://hadoop.apache.org/avro/">Avro</a>.
030     */
031    @Deprecated
032    @InterfaceAudience.Public
033    @InterfaceStability.Stable
034    public class SimpleCharStream
035    {
036      public static final boolean staticFlag = false;
037      int bufsize;
038      int available;
039      int tokenBegin;
040      public int bufpos = -1;
041      protected int bufline[];
042      protected int bufcolumn[];
043    
044      protected int column = 0;
045      protected int line = 1;
046    
047      protected boolean prevCharIsCR = false;
048      protected boolean prevCharIsLF = false;
049    
050      protected java.io.Reader inputStream;
051    
052      protected char[] buffer;
053      protected int maxNextCharInd = 0;
054      protected int inBuf = 0;
055      protected int tabSize = 8;
056    
057      protected void setTabSize(int i) { tabSize = i; }
058      protected int getTabSize(int i) { return tabSize; }
059    
060    
061      protected void ExpandBuff(boolean wrapAround)
062      {
063        char[] newbuffer = new char[bufsize + 2048];
064        int newbufline[] = new int[bufsize + 2048];
065        int newbufcolumn[] = new int[bufsize + 2048];
066    
067        try
068          {
069            if (wrapAround)
070              {
071                System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
072                System.arraycopy(buffer, 0, newbuffer,
073                                 bufsize - tokenBegin, bufpos);
074                buffer = newbuffer;
075    
076                System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
077                System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
078                bufline = newbufline;
079    
080                System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
081                System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
082                bufcolumn = newbufcolumn;
083    
084                maxNextCharInd = (bufpos += (bufsize - tokenBegin));
085              }
086            else
087              {
088                System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
089                buffer = newbuffer;
090    
091                System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
092                bufline = newbufline;
093    
094                System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
095                bufcolumn = newbufcolumn;
096    
097                maxNextCharInd = (bufpos -= tokenBegin);
098              }
099          }
100        catch (Throwable t)
101          {
102            throw new Error(t.getMessage());
103          }
104    
105    
106        bufsize += 2048;
107        available = bufsize;
108        tokenBegin = 0;
109      }
110    
111      protected void FillBuff() throws java.io.IOException
112      {
113        if (maxNextCharInd == available)
114          {
115            if (available == bufsize)
116              {
117                if (tokenBegin > 2048)
118                  {
119                    bufpos = maxNextCharInd = 0;
120                    available = tokenBegin;
121                  }
122                else if (tokenBegin < 0)
123                  bufpos = maxNextCharInd = 0;
124                else
125                  ExpandBuff(false);
126              }
127            else if (available > tokenBegin)
128              available = bufsize;
129            else if ((tokenBegin - available) < 2048)
130              ExpandBuff(true);
131            else
132              available = tokenBegin;
133          }
134    
135        int i;
136        try {
137          if ((i = inputStream.read(buffer, maxNextCharInd,
138                                    available - maxNextCharInd)) == -1)
139            {
140              inputStream.close();
141              throw new java.io.IOException();
142            }
143          else
144            maxNextCharInd += i;
145          return;
146        }
147        catch(java.io.IOException e) {
148          --bufpos;
149          backup(0);
150          if (tokenBegin == -1)
151            tokenBegin = bufpos;
152          throw e;
153        }
154      }
155    
156      public char BeginToken() throws java.io.IOException
157      {
158        tokenBegin = -1;
159        char c = readChar();
160        tokenBegin = bufpos;
161    
162        return c;
163      }
164    
165      protected void UpdateLineColumn(char c)
166      {
167        column++;
168    
169        if (prevCharIsLF)
170          {
171            prevCharIsLF = false;
172            line += (column = 1);
173          }
174        else if (prevCharIsCR)
175          {
176            prevCharIsCR = false;
177            if (c == '\n')
178              {
179                prevCharIsLF = true;
180              }
181            else
182              line += (column = 1);
183          }
184    
185        switch (c)
186          {
187          case '\r' :
188            prevCharIsCR = true;
189            break;
190          case '\n' :
191            prevCharIsLF = true;
192            break;
193          case '\t' :
194            column--;
195            column += (tabSize - (column % tabSize));
196            break;
197          default :
198            break;
199          }
200    
201        bufline[bufpos] = line;
202        bufcolumn[bufpos] = column;
203      }
204    
205      public char readChar() throws java.io.IOException
206      {
207        if (inBuf > 0)
208          {
209            --inBuf;
210    
211            if (++bufpos == bufsize)
212              bufpos = 0;
213    
214            return buffer[bufpos];
215          }
216    
217        if (++bufpos >= maxNextCharInd)
218          FillBuff();
219    
220        char c = buffer[bufpos];
221    
222        UpdateLineColumn(c);
223        return (c);
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, String encoding, int startline,
302                              int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
303      {
304        this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
305      }
306    
307      public SimpleCharStream(java.io.InputStream dstream, int startline,
308                              int startcolumn, int buffersize)
309      {
310        this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
311      }
312    
313      public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
314                              int startcolumn) throws java.io.UnsupportedEncodingException
315      {
316        this(dstream, encoding, startline, startcolumn, 4096);
317      }
318    
319      public SimpleCharStream(java.io.InputStream dstream, int startline,
320                              int startcolumn)
321      {
322        this(dstream, startline, startcolumn, 4096);
323      }
324    
325      public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
326      {
327        this(dstream, encoding, 1, 1, 4096);
328      }
329    
330      public SimpleCharStream(java.io.InputStream dstream)
331      {
332        this(dstream, 1, 1, 4096);
333      }
334    
335      public void ReInit(java.io.InputStream dstream, String encoding, int startline,
336                         int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
337      {
338        ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
339      }
340    
341      public void ReInit(java.io.InputStream dstream, int startline,
342                         int startcolumn, int buffersize)
343      {
344        ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
345      }
346    
347      public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
348      {
349        ReInit(dstream, encoding, 1, 1, 4096);
350      }
351    
352      public void ReInit(java.io.InputStream dstream)
353      {
354        ReInit(dstream, 1, 1, 4096);
355      }
356      public void ReInit(java.io.InputStream dstream, String encoding, int startline,
357                         int startcolumn) throws java.io.UnsupportedEncodingException
358      {
359        ReInit(dstream, encoding, startline, startcolumn, 4096);
360      }
361      public void ReInit(java.io.InputStream dstream, int startline,
362                         int startcolumn)
363      {
364        ReInit(dstream, startline, startcolumn, 4096);
365      }
366      public String GetImage()
367      {
368        if (bufpos >= tokenBegin)
369          return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
370        else
371          return new String(buffer, tokenBegin, bufsize - tokenBegin) +
372            new String(buffer, 0, bufpos + 1);
373      }
374    
375      public char[] GetSuffix(int len)
376      {
377        char[] ret = new char[len];
378    
379        if ((bufpos + 1) >= len)
380          System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
381        else
382          {
383            System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
384                             len - bufpos - 1);
385            System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
386          }
387    
388        return ret;
389      }
390    
391      public void Done()
392      {
393        buffer = null;
394        bufline = null;
395        bufcolumn = null;
396      }
397    
398      /**
399       * Method to adjust line and column numbers for the start of a token.
400       */
401      public void adjustBeginLineColumn(int newLine, int newCol)
402      {
403        int start = tokenBegin;
404        int len;
405    
406        if (bufpos >= tokenBegin)
407          {
408            len = bufpos - tokenBegin + inBuf + 1;
409          }
410        else
411          {
412            len = bufsize - tokenBegin + bufpos + 1 + inBuf;
413          }
414    
415        int i = 0, j = 0, k = 0;
416        int nextColDiff = 0, columnDiff = 0;
417    
418        while (i < len &&
419               bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
420          {
421            bufline[j] = newLine;
422            nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
423            bufcolumn[j] = newCol + columnDiff;
424            columnDiff = nextColDiff;
425            i++;
426          } 
427    
428        if (i < len)
429          {
430            bufline[j] = newLine++;
431            bufcolumn[j] = newCol + columnDiff;
432    
433            while (i++ < len)
434              {
435                if (bufline[j = start % bufsize] != bufline[++start % bufsize])
436                  bufline[j] = newLine++;
437                else
438                  bufline[j] = newLine;
439              }
440          }
441    
442        line = bufline[j];
443        column = bufcolumn[j];
444      }
445    
446    }