KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CharCollector


1
2 /*
3  * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
4  * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has
5  * intellectual property rights relating to technology embodied in the product
6  * that is described in this document. In particular, and without limitation,
7  * these intellectual property rights may include one or more of the U.S.
8  * patents listed at http://www.sun.com/patents and one or more additional
9  * patents or pending patent applications in the U.S. and in other countries.
10  * U.S. Government Rights - Commercial software. Government users are subject
11  * to the Sun Microsystems, Inc. standard license agreement and applicable
12  * provisions of the FAR and its supplements. Use is subject to license terms.
13  * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
14  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This
15  * product is covered and controlled by U.S. Export Control laws and may be
16  * subject to the export or import laws in other countries. Nuclear, missile,
17  * chemical biological weapons or nuclear maritime end uses or end users,
18  * whether direct or indirect, are strictly prohibited. Export or reexport
19  * to countries subject to U.S. embargo or to entities identified on U.S.
20  * export exclusion lists, including, but not limited to, the denied persons
21  * and specially designated nationals lists is strictly prohibited.
22  */

23
24
25 public class CharCollector implements CharStream {
26
27   int bufsize;
28   int available;
29   int tokenBegin;
30   public int bufpos = -1;
31   private char[] buffer;
32   private int maxNextCharInd = 0;
33
34   private final void ExpandBuff(boolean wrapAround)
35   {
36      char[] newbuffer = new char[bufsize + 2048];
37
38      try
39      {
40         if (wrapAround)
41         {
42            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
43            System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
44            buffer = newbuffer;
45            maxNextCharInd = (bufpos += (bufsize - tokenBegin));
46         }
47         else
48         {
49            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
50            buffer = newbuffer;
51            maxNextCharInd = (bufpos -= tokenBegin);
52         }
53      }
54      catch (Throwable JavaDoc t)
55      {
56         System.out.println("Error : " + t.getClass().getName());
57         throw new Error JavaDoc();
58      }
59
60      bufsize += 2048;
61      available = bufsize;
62      tokenBegin = 0;
63   }
64
65   private final void FillBuff()
66   {
67      if (maxNextCharInd == available)
68      {
69         if (available == bufsize)
70         {
71            if (tokenBegin > 2048)
72            {
73               bufpos = maxNextCharInd = 0;
74               available = tokenBegin;
75            }
76            else if (tokenBegin < 0)
77               bufpos = maxNextCharInd = 0;
78            else
79               ExpandBuff(false);
80         }
81         else if (available > tokenBegin)
82            available = bufsize;
83         else if ((tokenBegin - available) < 2048)
84            ExpandBuff(true);
85         else
86            available = tokenBegin;
87      }
88
89      try {
90        wait();
91      } catch (InterruptedException JavaDoc willNotHappen) {
92        throw new Error JavaDoc();
93      }
94   }
95
96   /**
97    * Puts a character into the buffer.
98    */

99   synchronized public final void put(char c)
100   {
101      buffer[maxNextCharInd++] = c;
102      notify();
103   }
104
105   public char BeginToken() throws java.io.IOException JavaDoc
106   {
107      tokenBegin = -1;
108      char c = readChar();
109      tokenBegin = bufpos;
110
111      return c;
112   }
113
114   private int inBuf = 0;
115   synchronized public final char readChar() throws java.io.IOException JavaDoc
116   {
117      if (inBuf > 0)
118      {
119         --inBuf;
120         return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]);
121      }
122
123      if (++bufpos >= maxNextCharInd)
124         FillBuff();
125
126      return buffer[bufpos];
127   }
128
129   /**
130    * @deprecated
131    * @see #getEndColumn
132    */

133
134   public final int getColumn() {
135       return 0;
136   }
137
138   /**
139    * @deprecated
140    * @see #getEndLine
141    */

142
143   public final int getLine() {
144       return 0;
145   }
146
147   public final int getEndColumn() {
148       return 0;
149   }
150
151   public final int getEndLine() {
152       return 0;
153   }
154
155   public final int getBeginColumn() {
156       return 0;
157   }
158
159   public final int getBeginLine() {
160       return 0;
161   }
162
163   public final void backup(int amount) {
164
165     inBuf += amount;
166     if ((bufpos -= amount) < 0)
167        bufpos += bufsize;
168   }
169
170   public CharCollector(int buffersize)
171   {
172     available = bufsize = buffersize;
173     buffer = new char[buffersize];
174   }
175
176   public CharCollector()
177   {
178     available = bufsize = 4096;
179     buffer = new char[4096];
180   }
181
182   public void Clear()
183   {
184      bufpos = -1;
185      maxNextCharInd = 0;
186      inBuf = 0;
187   }
188
189   public final String JavaDoc GetImage()
190   {
191      if (bufpos >= tokenBegin)
192         return new String JavaDoc(buffer, tokenBegin, bufpos - tokenBegin + 1);
193      else
194         return new String JavaDoc(buffer, tokenBegin, bufsize - tokenBegin) +
195                               new String JavaDoc(buffer, 0, bufpos + 1);
196   }
197
198   public final char[] GetSuffix(int len)
199   {
200      char[] ret = new char[len];
201
202      if (bufpos + 1 >= len)
203         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
204      else
205      {
206         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
207                                                           len - bufpos - 1);
208         System.arraycopy(buffer, 0, ret, len - bufpos, bufpos + 1);
209      }
210
211      return ret;
212   }
213
214   public void Done()
215   {
216      buffer = null;
217   }
218 }
219
Popular Tags