001    /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 2.1 */
002    package org.activemq.selector;
003    
004    /**
005     * An implementation of interface CharStream, where the stream is assumed to
006     * contain only ASCII characters (without unicode processing).
007     */
008    
009    public final class SimpleCharStream {
010        public static final boolean staticFlag = false;
011        int bufsize;
012        int available;
013        int tokenBegin;
014        public int bufpos = -1;
015        private int bufline[];
016        private int bufcolumn[];
017    
018        private int column = 0;
019        private int line = 1;
020    
021        private boolean prevCharIsCR = false;
022        private boolean prevCharIsLF = false;
023    
024        private java.io.Reader inputStream;
025    
026        private char[] buffer;
027        private int maxNextCharInd = 0;
028        private int inBuf = 0;
029    
030        private final void ExpandBuff(boolean wrapAround) {
031            char[] newbuffer = new char[bufsize + 2048];
032            int newbufline[] = new int[bufsize + 2048];
033            int newbufcolumn[] = new int[bufsize + 2048];
034    
035            try {
036                if (wrapAround) {
037                    System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
038                    System.arraycopy(buffer, 0, newbuffer,
039                            bufsize - tokenBegin, bufpos);
040                    buffer = newbuffer;
041    
042                    System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
043                    System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
044                    bufline = newbufline;
045    
046                    System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
047                    System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
048                    bufcolumn = newbufcolumn;
049    
050                    maxNextCharInd = (bufpos += (bufsize - tokenBegin));
051                }
052                else {
053                    System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
054                    buffer = newbuffer;
055    
056                    System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
057                    bufline = newbufline;
058    
059                    System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
060                    bufcolumn = newbufcolumn;
061    
062                    maxNextCharInd = (bufpos -= tokenBegin);
063                }
064            }
065            catch (Throwable t) {
066                throw new Error(t.getMessage());
067            }
068    
069    
070            bufsize += 2048;
071            available = bufsize;
072            tokenBegin = 0;
073        }
074    
075        private final void FillBuff() throws java.io.IOException {
076            if (maxNextCharInd == available) {
077                if (available == bufsize) {
078                    if (tokenBegin > 2048) {
079                        bufpos = maxNextCharInd = 0;
080                        available = tokenBegin;
081                    }
082                    else if (tokenBegin < 0) {
083                        bufpos = maxNextCharInd = 0;
084                    }
085                    else {
086                        ExpandBuff(false);
087                    }
088                }
089                else if (available > tokenBegin) {
090                    available = bufsize;
091                }
092                else if ((tokenBegin - available) < 2048) {
093                    ExpandBuff(true);
094                }
095                else {
096                    available = tokenBegin;
097                }
098            }
099    
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    }