KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > expr > SimpleCharStream


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 3.0 */
21 package org.netbeans.modules.debugger.jpda.expr;
22
23 /**
24  * An implementation of interface CharStream, where the stream is assumed to
25  * contain only ASCII characters (without unicode processing).
26  */

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

212
213   public int getColumn() {
214      return bufcolumn[bufpos];
215   }
216
217   /**
218    * @deprecated
219    * @see #getEndLine
220    */

221
222   public int getLine() {
223      return bufline[bufpos];
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 JavaDoc 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 JavaDoc dstream, int startline,
263                                                            int startcolumn)
264   {
265      this(dstream, startline, startcolumn, 4096);
266   }
267
268   public SimpleCharStream(java.io.Reader JavaDoc dstream)
269   {
270      this(dstream, 1, 1, 4096);
271   }
272   public void ReInit(java.io.Reader JavaDoc 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 JavaDoc dstream, int startline,
292                                                            int startcolumn)
293   {
294      ReInit(dstream, startline, startcolumn, 4096);
295   }
296
297   public void ReInit(java.io.Reader JavaDoc dstream)
298   {
299      ReInit(dstream, 1, 1, 4096);
300   }
301   public SimpleCharStream(java.io.InputStream JavaDoc dstream, int startline,
302   int startcolumn, int buffersize)
303   {
304      this(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
305   }
306
307   public SimpleCharStream(java.io.InputStream JavaDoc dstream, int startline,
308                                                            int startcolumn)
309   {
310      this(dstream, startline, startcolumn, 4096);
311   }
312
313   public SimpleCharStream(java.io.InputStream JavaDoc dstream)
314   {
315      this(dstream, 1, 1, 4096);
316   }
317
318   public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
319                           int startcolumn, int buffersize)
320   {
321      ReInit(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
322   }
323
324   public void ReInit(java.io.InputStream JavaDoc dstream)
325   {
326      ReInit(dstream, 1, 1, 4096);
327   }
328   public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
329                                                            int startcolumn)
330   {
331      ReInit(dstream, startline, startcolumn, 4096);
332   }
333   public String JavaDoc GetImage()
334   {
335      if (bufpos >= tokenBegin)
336         return new String JavaDoc(buffer, tokenBegin, bufpos - tokenBegin + 1);
337      else
338         return new String JavaDoc(buffer, tokenBegin, bufsize - tokenBegin) +
339                               new String JavaDoc(buffer, 0, bufpos + 1);
340   }
341
342   public char[] GetSuffix(int len)
343   {
344      char[] ret = new char[len];
345
346      if ((bufpos + 1) >= len)
347         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
348      else
349      {
350         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
351                                                           len - bufpos - 1);
352         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
353      }
354
355      return ret;
356   }
357
358   public void Done()
359   {
360      buffer = null;
361      bufline = null;
362      bufcolumn = null;
363   }
364
365   /**
366    * Method to adjust line and column numbers for the start of a token.
367    */

368   public void adjustBeginLineColumn(int newLine, int newCol)
369   {
370      int start = tokenBegin;
371      int len;
372
373      if (bufpos >= tokenBegin)
374      {
375         len = bufpos - tokenBegin + inBuf + 1;
376      }
377      else
378      {
379         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
380      }
381
382      int i = 0, j = 0, k = 0;
383      int nextColDiff = 0, columnDiff = 0;
384
385      while (i < len &&
386             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
387      {
388         bufline[j] = newLine;
389         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
390         bufcolumn[j] = newCol + columnDiff;
391         columnDiff = nextColDiff;
392         i++;
393      }
394
395      if (i < len)
396      {
397         bufline[j] = newLine++;
398         bufcolumn[j] = newCol + columnDiff;
399
400         while (i++ < len)
401         {
402            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
403               bufline[j] = newLine++;
404            else
405               bufline[j] = newLine;
406         }
407      }
408
409      line = bufline[j];
410      column = bufcolumn[j];
411   }
412
413 }
414
Popular Tags