KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > IPAcl > ASCII_CharStream


1 /*
2  * @(#)ASCII_CharStream.java 4.16 04/07/26
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 /* Generated By:JavaCC: Do not edit this line. ASCII_CharStream.java Version 0.7pre6 */
9 package com.sun.jmx.snmp.IPAcl;
10
11 /**
12  * An implementation of interface CharStream, where the stream is assumed to
13  * contain only ASCII characters (without unicode processing).
14  */

15
16 final class ASCII_CharStream
17 {
18   public static final boolean staticFlag = false;
19   int bufsize;
20   int available;
21   int tokenBegin;
22   public int bufpos = -1;
23   private int bufline[];
24   private int bufcolumn[];
25
26   private int column = 0;
27   private int line = 1;
28
29   private boolean prevCharIsCR = false;
30   private boolean prevCharIsLF = false;
31
32   private java.io.Reader JavaDoc inputStream;
33
34   private char[] buffer;
35   private int maxNextCharInd = 0;
36   private int inBuf = 0;
37
38   private final void ExpandBuff(boolean wrapAround)
39   {
40      char[] newbuffer = new char[bufsize + 2048];
41      int newbufline[] = new int[bufsize + 2048];
42      int newbufcolumn[] = new int[bufsize + 2048];
43
44      try
45      {
46         if (wrapAround)
47         {
48            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
49            System.arraycopy(buffer, 0, newbuffer,
50                                              bufsize - tokenBegin, bufpos);
51            buffer = newbuffer;
52
53            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
54            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
55            bufline = newbufline;
56
57            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
58            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
59            bufcolumn = newbufcolumn;
60
61            maxNextCharInd = (bufpos += (bufsize - tokenBegin));
62         }
63         else
64         {
65            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
66            buffer = newbuffer;
67
68            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
69            bufline = newbufline;
70
71            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
72            bufcolumn = newbufcolumn;
73
74            maxNextCharInd = (bufpos -= tokenBegin);
75         }
76      }
77      catch (Throwable JavaDoc t)
78      {
79         throw new Error JavaDoc(t.getMessage());
80      }
81
82
83      bufsize += 2048;
84      available = bufsize;
85      tokenBegin = 0;
86   }
87
88   private final void FillBuff() throws java.io.IOException JavaDoc
89   {
90      if (maxNextCharInd == available)
91      {
92         if (available == bufsize)
93         {
94            if (tokenBegin > 2048)
95            {
96               bufpos = maxNextCharInd = 0;
97               available = tokenBegin;
98            }
99            else if (tokenBegin < 0)
100               bufpos = maxNextCharInd = 0;
101            else
102               ExpandBuff(false);
103         }
104         else if (available > tokenBegin)
105            available = bufsize;
106         else if ((tokenBegin - available) < 2048)
107            ExpandBuff(true);
108         else
109            available = tokenBegin;
110      }
111
112      int i;
113      try {
114         if ((i = inputStream.read(buffer, maxNextCharInd,
115                                     available - maxNextCharInd)) == -1)
116         {
117            inputStream.close();
118            throw new java.io.IOException JavaDoc();
119         }
120         else
121            maxNextCharInd += i;
122         return;
123      }
124      catch(java.io.IOException JavaDoc e) {
125         --bufpos;
126         backup(0);
127         if (tokenBegin == -1)
128            tokenBegin = bufpos;
129         throw e;
130      }
131   }
132
133   public final char BeginToken() throws java.io.IOException JavaDoc
134   {
135      tokenBegin = -1;
136      char c = readChar();
137      tokenBegin = bufpos;
138
139      return c;
140   }
141
142   private final void UpdateLineColumn(char c)
143   {
144      column++;
145
146      if (prevCharIsLF)
147      {
148         prevCharIsLF = false;
149         line += (column = 1);
150      }
151      else if (prevCharIsCR)
152      {
153         prevCharIsCR = false;
154         if (c == '\n')
155         {
156            prevCharIsLF = true;
157         }
158         else
159            line += (column = 1);
160      }
161
162      switch (c)
163      {
164         case '\r' :
165            prevCharIsCR = true;
166            break;
167         case '\n' :
168            prevCharIsLF = true;
169            break;
170         case '\t' :
171            column--;
172            column += (8 - (column & 07));
173            break;
174         default :
175            break;
176      }
177
178      bufline[bufpos] = line;
179      bufcolumn[bufpos] = column;
180   }
181
182   public final char readChar() throws java.io.IOException JavaDoc
183   {
184      if (inBuf > 0)
185      {
186         --inBuf;
187         return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]);
188      }
189
190      if (++bufpos >= maxNextCharInd)
191         FillBuff();
192
193      char c = (char)((char)0xff & buffer[bufpos]);
194
195      UpdateLineColumn(c);
196      return (c);
197   }
198
199   /**
200    * @deprecated
201    * @see #getEndColumn
202    */

203     @Deprecated JavaDoc
204   public final int getColumn() {
205      return bufcolumn[bufpos];
206   }
207
208   /**
209    * @deprecated
210    * @see #getEndLine
211    */

212     @Deprecated JavaDoc
213   public final int getLine() {
214      return bufline[bufpos];
215   }
216
217   public final int getEndColumn() {
218      return bufcolumn[bufpos];
219   }
220
221   public final int getEndLine() {
222      return bufline[bufpos];
223   }
224
225   public final int getBeginColumn() {
226      return bufcolumn[tokenBegin];
227   }
228
229   public final int getBeginLine() {
230      return bufline[tokenBegin];
231   }
232
233   public final void backup(int amount) {
234
235     inBuf += amount;
236     if ((bufpos -= amount) < 0)
237        bufpos += bufsize;
238   }
239
240   public ASCII_CharStream(java.io.Reader JavaDoc dstream, int startline,
241   int startcolumn, int buffersize)
242   {
243     inputStream = dstream;
244     line = startline;
245     column = startcolumn - 1;
246
247     available = bufsize = buffersize;
248     buffer = new char[buffersize];
249     bufline = new int[buffersize];
250     bufcolumn = new int[buffersize];
251   }
252
253   public ASCII_CharStream(java.io.Reader JavaDoc dstream, int startline,
254                                                            int startcolumn)
255   {
256      this(dstream, startline, startcolumn, 4096);
257   }
258   public void ReInit(java.io.Reader JavaDoc dstream, int startline,
259   int startcolumn, int buffersize)
260   {
261     inputStream = dstream;
262     line = startline;
263     column = startcolumn - 1;
264
265     if (buffer == null || buffersize != buffer.length)
266     {
267       available = bufsize = buffersize;
268       buffer = new char[buffersize];
269       bufline = new int[buffersize];
270       bufcolumn = new int[buffersize];
271     }
272     prevCharIsLF = prevCharIsCR = false;
273     tokenBegin = inBuf = maxNextCharInd = 0;
274     bufpos = -1;
275   }
276
277   public void ReInit(java.io.Reader JavaDoc dstream, int startline,
278                                                            int startcolumn)
279   {
280      ReInit(dstream, startline, startcolumn, 4096);
281   }
282   public ASCII_CharStream(java.io.InputStream JavaDoc dstream, int startline,
283   int startcolumn, int buffersize)
284   {
285      this(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
286   }
287
288   public ASCII_CharStream(java.io.InputStream JavaDoc dstream, int startline,
289                                                            int startcolumn)
290   {
291      this(dstream, startline, startcolumn, 4096);
292   }
293
294   public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
295   int startcolumn, int buffersize)
296   {
297      ReInit(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
298   }
299   public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
300                                                            int startcolumn)
301   {
302      ReInit(dstream, startline, startcolumn, 4096);
303   }
304   public final String JavaDoc GetImage()
305   {
306      if (bufpos >= tokenBegin)
307         return new String JavaDoc(buffer, tokenBegin, bufpos - tokenBegin + 1);
308      else
309         return new String JavaDoc(buffer, tokenBegin, bufsize - tokenBegin) +
310                               new String JavaDoc(buffer, 0, bufpos + 1);
311   }
312
313   public final char[] GetSuffix(int len)
314   {
315      char[] ret = new char[len];
316
317      if ((bufpos + 1) >= len)
318         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
319      else
320      {
321         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
322                                                           len - bufpos - 1);
323         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
324      }
325
326      return ret;
327   }
328
329   public void Done()
330   {
331      buffer = null;
332      bufline = null;
333      bufcolumn = null;
334   }
335
336   /**
337    * Method to adjust line and column numbers for the start of a token.
338    */

339   public void adjustBeginLineColumn(int newLine, int newCol)
340   {
341      int start = tokenBegin;
342      int len;
343
344      if (bufpos >= tokenBegin)
345      {
346         len = bufpos - tokenBegin + inBuf + 1;
347      }
348      else
349      {
350         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
351      }
352
353      int i = 0, j = 0, k = 0;
354      int nextColDiff = 0, columnDiff = 0;
355
356      while (i < len &&
357             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
358      {
359         bufline[j] = newLine;
360         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
361         bufcolumn[j] = newCol + columnDiff;
362         columnDiff = nextColDiff;
363         i++;
364      }
365
366      if (i < len)
367      {
368         bufline[j] = newLine++;
369         bufcolumn[j] = newCol + columnDiff;
370
371         while (i++ < len)
372         {
373            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
374               bufline[j] = newLine++;
375            else
376               bufline[j] = newLine;
377         }
378      }
379
380      line = bufline[j];
381      column = bufcolumn[j];
382   }
383
384 }
385
Popular Tags