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