View Javadoc

1   /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 2.1 */
2   package org.codehaus.activemq.selector;
3   
4   /***
5    * An implementation of interface CharStream, where the stream is assumed to
6    * contain only ASCII characters (without unicode processing).
7    */
8   
9   public final class SimpleCharStream {
10      public static final boolean staticFlag = false;
11      int bufsize;
12      int available;
13      int tokenBegin;
14      public int bufpos = -1;
15      private int bufline[];
16      private int bufcolumn[];
17  
18      private int column = 0;
19      private int line = 1;
20  
21      private boolean prevCharIsCR = false;
22      private boolean prevCharIsLF = false;
23  
24      private java.io.Reader inputStream;
25  
26      private char[] buffer;
27      private int maxNextCharInd = 0;
28      private int inBuf = 0;
29  
30      private final void ExpandBuff(boolean wrapAround) {
31          char[] newbuffer = new char[bufsize + 2048];
32          int newbufline[] = new int[bufsize + 2048];
33          int newbufcolumn[] = new int[bufsize + 2048];
34  
35          try {
36              if (wrapAround) {
37                  System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
38                  System.arraycopy(buffer, 0, newbuffer,
39                          bufsize - tokenBegin, bufpos);
40                  buffer = newbuffer;
41  
42                  System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
43                  System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
44                  bufline = newbufline;
45  
46                  System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
47                  System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
48                  bufcolumn = newbufcolumn;
49  
50                  maxNextCharInd = (bufpos += (bufsize - tokenBegin));
51              }
52              else {
53                  System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
54                  buffer = newbuffer;
55  
56                  System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
57                  bufline = newbufline;
58  
59                  System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
60                  bufcolumn = newbufcolumn;
61  
62                  maxNextCharInd = (bufpos -= tokenBegin);
63              }
64          }
65          catch (Throwable t) {
66              throw new Error(t.getMessage());
67          }
68  
69  
70          bufsize += 2048;
71          available = bufsize;
72          tokenBegin = 0;
73      }
74  
75      private final void FillBuff() throws java.io.IOException {
76          if (maxNextCharInd == available) {
77              if (available == bufsize) {
78                  if (tokenBegin > 2048) {
79                      bufpos = maxNextCharInd = 0;
80                      available = tokenBegin;
81                  }
82                  else if (tokenBegin < 0) {
83                      bufpos = maxNextCharInd = 0;
84                  }
85                  else {
86                      ExpandBuff(false);
87                  }
88              }
89              else if (available > tokenBegin) {
90                  available = bufsize;
91              }
92              else if ((tokenBegin - available) < 2048) {
93                  ExpandBuff(true);
94              }
95              else {
96                  available = tokenBegin;
97              }
98          }
99  
100         int i;
101         try {
102             if ((i = inputStream.read(buffer, maxNextCharInd,
103                     available - maxNextCharInd)) == -1) {
104                 inputStream.close();
105                 throw new java.io.IOException();
106             }
107             else {
108                 maxNextCharInd += i;
109             }
110             return;
111         }
112         catch (java.io.IOException e) {
113             --bufpos;
114             backup(0);
115             if (tokenBegin == -1) {
116                 tokenBegin = bufpos;
117             }
118             throw e;
119         }
120     }
121 
122     public final char BeginToken() throws java.io.IOException {
123         tokenBegin = -1;
124         char c = readChar();
125         tokenBegin = bufpos;
126 
127         return c;
128     }
129 
130     private final void UpdateLineColumn(char c) {
131         column++;
132 
133         if (prevCharIsLF) {
134             prevCharIsLF = false;
135             line += (column = 1);
136         }
137         else if (prevCharIsCR) {
138             prevCharIsCR = false;
139             if (c == '\n') {
140                 prevCharIsLF = true;
141             }
142             else {
143                 line += (column = 1);
144             }
145         }
146 
147         switch (c) {
148             case '\r':
149                 prevCharIsCR = true;
150                 break;
151             case '\n':
152                 prevCharIsLF = true;
153                 break;
154             case '\t':
155                 column--;
156                 column += (8 - (column & 07));
157                 break;
158             default :
159                 break;
160         }
161 
162         bufline[bufpos] = line;
163         bufcolumn[bufpos] = column;
164     }
165 
166     public final char readChar() throws java.io.IOException {
167         if (inBuf > 0) {
168             --inBuf;
169 
170             if (++bufpos == bufsize) {
171                 bufpos = 0;
172             }
173 
174             return buffer[bufpos];
175         }
176 
177         if (++bufpos >= maxNextCharInd) {
178             FillBuff();
179         }
180 
181         char c = buffer[bufpos];
182 
183         UpdateLineColumn(c);
184         return (c);
185     }
186 
187     /***
188      * @see #getEndColumn
189      * @deprecated
190      */
191 
192     public final int getColumn() {
193         return bufcolumn[bufpos];
194     }
195 
196     /***
197      * @see #getEndLine
198      * @deprecated
199      */
200 
201     public final int getLine() {
202         return bufline[bufpos];
203     }
204 
205     public final int getEndColumn() {
206         return bufcolumn[bufpos];
207     }
208 
209     public final int getEndLine() {
210         return bufline[bufpos];
211     }
212 
213     public final int getBeginColumn() {
214         return bufcolumn[tokenBegin];
215     }
216 
217     public final int getBeginLine() {
218         return bufline[tokenBegin];
219     }
220 
221     public final void backup(int amount) {
222 
223         inBuf += amount;
224         if ((bufpos -= amount) < 0) {
225             bufpos += bufsize;
226         }
227     }
228 
229     public SimpleCharStream(java.io.Reader dstream, int startline,
230                             int startcolumn, int buffersize) {
231         inputStream = dstream;
232         line = startline;
233         column = startcolumn - 1;
234 
235         available = bufsize = buffersize;
236         buffer = new char[buffersize];
237         bufline = new int[buffersize];
238         bufcolumn = new int[buffersize];
239     }
240 
241     public SimpleCharStream(java.io.Reader dstream, int startline,
242                             int startcolumn) {
243         this(dstream, startline, startcolumn, 4096);
244     }
245 
246     public SimpleCharStream(java.io.Reader dstream) {
247         this(dstream, 1, 1, 4096);
248     }
249 
250     public void ReInit(java.io.Reader dstream, int startline,
251                        int startcolumn, int buffersize) {
252         inputStream = dstream;
253         line = startline;
254         column = startcolumn - 1;
255 
256         if (buffer == null || buffersize != buffer.length) {
257             available = bufsize = buffersize;
258             buffer = new char[buffersize];
259             bufline = new int[buffersize];
260             bufcolumn = new int[buffersize];
261         }
262         prevCharIsLF = prevCharIsCR = false;
263         tokenBegin = inBuf = maxNextCharInd = 0;
264         bufpos = -1;
265     }
266 
267     public void ReInit(java.io.Reader dstream, int startline,
268                        int startcolumn) {
269         ReInit(dstream, startline, startcolumn, 4096);
270     }
271 
272     public void ReInit(java.io.Reader dstream) {
273         ReInit(dstream, 1, 1, 4096);
274     }
275 
276     public SimpleCharStream(java.io.InputStream dstream, int startline,
277                             int startcolumn, int buffersize) {
278         this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
279     }
280 
281     public SimpleCharStream(java.io.InputStream dstream, int startline,
282                             int startcolumn) {
283         this(dstream, startline, startcolumn, 4096);
284     }
285 
286     public SimpleCharStream(java.io.InputStream dstream) {
287         this(dstream, 1, 1, 4096);
288     }
289 
290     public void ReInit(java.io.InputStream dstream, int startline,
291                        int startcolumn, int buffersize) {
292         ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
293     }
294 
295     public void ReInit(java.io.InputStream dstream) {
296         ReInit(dstream, 1, 1, 4096);
297     }
298 
299     public void ReInit(java.io.InputStream dstream, int startline,
300                        int startcolumn) {
301         ReInit(dstream, startline, startcolumn, 4096);
302     }
303 
304     public final String GetImage() {
305         if (bufpos >= tokenBegin) {
306             return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
307         }
308         else {
309             return new String(buffer, tokenBegin, bufsize - tokenBegin) +
310                     new String(buffer, 0, bufpos + 1);
311         }
312     }
313 
314     public final char[] GetSuffix(int len) {
315         char[] ret = new char[len];
316 
317         if ((bufpos + 1) >= len) {
318             System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
319         }
320         else {
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         buffer = null;
331         bufline = null;
332         bufcolumn = null;
333     }
334 
335     /***
336      * Method to adjust line and column numbers for the start of a token.<BR>
337      */
338     public void adjustBeginLineColumn(int newLine, int newCol) {
339         int start = tokenBegin;
340         int len;
341 
342         if (bufpos >= tokenBegin) {
343             len = bufpos - tokenBegin + inBuf + 1;
344         }
345         else {
346             len = bufsize - tokenBegin + bufpos + 1 + inBuf;
347         }
348 
349         int i = 0, j = 0, k = 0;
350         int nextColDiff = 0, columnDiff = 0;
351 
352         while (i < len &&
353                 bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) {
354             bufline[j] = newLine;
355             nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
356             bufcolumn[j] = newCol + columnDiff;
357             columnDiff = nextColDiff;
358             i++;
359         }
360 
361         if (i < len) {
362             bufline[j] = newLine++;
363             bufcolumn[j] = newCol + columnDiff;
364 
365             while (i++ < len) {
366                 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) {
367                     bufline[j] = newLine++;
368                 }
369                 else {
370                     bufline[j] = newLine;
371                 }
372             }
373         }
374 
375         line = bufline[j];
376         column = bufcolumn[j];
377     }
378 
379 }