1 package nom.tam.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 @Deprecated
57 public class ByteParser {
58
59 private static final int EXPONENT_DENORMALISATION_CORR_LIMIT = -300;
60
61 private static final double EXPONENT_DENORMALISATION_FACTOR = 1.e-300;
62
63 private static final byte[] INFINITY_LOWER = AsciiFuncs.getBytes(ByteFormatter.INFINITY.toLowerCase());
64
65 private static final byte[] INFINITY_UPPER = AsciiFuncs.getBytes(ByteFormatter.INFINITY.toUpperCase());
66
67 private static final int INFINITY_LENGTH = ByteParser.INFINITY_UPPER.length;
68
69 private static final int INFINITY_SHORTCUT_LENGTH = 3;
70
71 private static final byte[] NOT_A_NUMBER_LOWER = AsciiFuncs.getBytes(ByteFormatter.NOT_A_NUMBER.toLowerCase());
72
73 private static final byte[] NOT_A_NUMBER_UPPER = AsciiFuncs.getBytes(ByteFormatter.NOT_A_NUMBER.toUpperCase());
74
75 private static final int NOT_A_NUMBER_LENGTH = ByteParser.NOT_A_NUMBER_UPPER.length;
76
77
78
79
80 private static final int NUMBER_BASE = 10;
81
82
83
84
85 private static final double NUMBER_BASE_DOUBLE = 10.;
86
87
88
89
90 private boolean foundSign;
91
92
93
94
95 private byte[] input;
96
97
98
99
100 private int numberLength;
101
102
103
104
105 private int offset;
106
107
108
109
110
111
112
113 public ByteParser(byte[] input) {
114 this.input = input;
115 offset = 0;
116 }
117
118
119
120
121
122 private int checkSign() {
123
124 foundSign = false;
125
126 if (input[offset] == '+') {
127 foundSign = true;
128 offset++;
129 return 1;
130 }
131 if (input[offset] == '-') {
132 foundSign = true;
133 offset++;
134 return -1;
135 }
136
137 return 1;
138 }
139
140
141
142
143
144
145
146
147 private double getBareInteger(int length) {
148
149 int startOffset = offset;
150 double number = 0;
151
152 while (length > 0 && input[offset] >= '0' && input[offset] <= '9') {
153
154 number *= ByteParser.NUMBER_BASE;
155 number += input[offset] - '0';
156 offset++;
157 length--;
158 }
159 numberLength = offset - startOffset;
160 return number;
161 }
162
163
164
165
166
167
168 public boolean getBoolean() throws FormatException {
169 return getBoolean(input.length - offset);
170 }
171
172
173
174
175
176
177
178
179 public boolean getBoolean(int length) throws FormatException {
180
181 int startOffset = offset;
182 length -= skipWhite(length);
183 if (length == 0) {
184 throw new FormatException("Blank boolean field");
185 }
186
187 boolean value = false;
188 if (input[offset] == 'T' || input[offset] == 't') {
189 value = true;
190 } else if (input[offset] != 'F' && input[offset] != 'f') {
191 numberLength = 0;
192 offset = startOffset;
193 throw new FormatException("Invalid boolean value");
194 }
195 offset++;
196 numberLength = offset - startOffset;
197 return value;
198 }
199
200
201
202
203 public byte[] getBuffer() {
204 return input;
205 }
206
207
208
209
210
211
212
213
214 public double getDouble() throws FormatException {
215 return getDouble(input.length - offset);
216 }
217
218
219
220
221
222
223
224
225
226 public double getDouble(int length) throws FormatException {
227 int startOffset = offset;
228 boolean error = true;
229 double number;
230
231 length -= skipWhite(length);
232 if (length == 0) {
233 numberLength = offset - startOffset;
234 return 0;
235 }
236 double mantissaSign = checkSign();
237 if (foundSign) {
238 length--;
239 }
240
241 if (isCaseInsensitiv(length, ByteParser.NOT_A_NUMBER_LENGTH, ByteParser.NOT_A_NUMBER_LOWER,
242 ByteParser.NOT_A_NUMBER_UPPER)) {
243 number = Double.NaN;
244 offset += ByteParser.NOT_A_NUMBER_LENGTH;
245
246 } else if (isCaseInsensitiv(length, ByteParser.INFINITY_LENGTH, ByteParser.INFINITY_LOWER,
247 ByteParser.INFINITY_UPPER)) {
248 number = Double.POSITIVE_INFINITY;
249 offset += ByteParser.INFINITY_LENGTH;
250 } else if (isCaseInsensitiv(length, ByteParser.INFINITY_SHORTCUT_LENGTH, ByteParser.INFINITY_LOWER,
251 ByteParser.INFINITY_UPPER)) {
252 number = Double.POSITIVE_INFINITY;
253 offset += ByteParser.INFINITY_SHORTCUT_LENGTH;
254 } else {
255 number = getBareInteger(length);
256 length -= numberLength;
257 if (numberLength > 0) {
258 error = false;
259 }
260
261 if (length > 0 && input[offset] == '.') {
262 offset++;
263 length--;
264 double numerator = getBareInteger(length);
265 if (numerator > 0) {
266 number += numerator / Math.pow(ByteParser.NUMBER_BASE_DOUBLE, numberLength);
267 }
268 length -= numberLength;
269 if (numberLength > 0) {
270 error = false;
271 }
272 }
273
274 if (error) {
275 offset = startOffset;
276 numberLength = 0;
277 throw new FormatException("Invalid real field");
278 }
279
280
281
282
283 if (length > 0
284 && (input[offset] == 'e' || input[offset] == 'E' || input[offset] == 'd' || input[offset] == 'D')) {
285 offset++;
286 length--;
287 if (length > 0) {
288 int sign = checkSign();
289 if (foundSign) {
290 length--;
291 }
292
293 int exponent = (int) getBareInteger(length);
294
295
296
297 if (exponent * sign > ByteParser.EXPONENT_DENORMALISATION_CORR_LIMIT) {
298 number *= Math.pow(ByteParser.NUMBER_BASE_DOUBLE, exponent * sign);
299 } else {
300 number = ByteParser.EXPONENT_DENORMALISATION_FACTOR
301 * (number * Math.pow(ByteParser.NUMBER_BASE_DOUBLE,
302 exponent * sign + ByteParser.EXPONENT_DENORMALISATION_CORR_LIMIT * -1));
303 }
304 }
305 }
306 }
307 numberLength = offset - startOffset;
308 return mantissaSign * number;
309 }
310
311
312
313
314
315
316 public float getFloat() throws FormatException {
317 return (float) getDouble(input.length - offset);
318 }
319
320
321
322
323
324
325
326
327 public float getFloat(int length) throws FormatException {
328 return (float) getDouble(length);
329 }
330
331
332
333
334
335
336 public int getInt() throws FormatException {
337 return getInt(input.length - offset);
338 }
339
340
341
342
343
344
345
346
347
348 public int getInt(int length) throws FormatException {
349 int startOffset = offset;
350
351 length -= skipWhite(length);
352 if (length == 0) {
353 numberLength = offset - startOffset;
354 return 0;
355 }
356
357 int number = 0;
358 boolean error = true;
359
360 int sign = checkSign();
361 if (foundSign) {
362 length--;
363 }
364
365 while (length > 0 && input[offset] >= '0' && input[offset] <= '9') {
366 number = number * ByteParser.NUMBER_BASE + input[offset] - '0';
367 offset++;
368 length--;
369 error = false;
370 }
371
372 if (error) {
373 numberLength = 0;
374 offset = startOffset;
375 throw new FormatException("Invalid Integer");
376 }
377 numberLength = offset - startOffset;
378 return sign * number;
379 }
380
381
382
383
384
385
386
387
388 public long getLong(int length) throws FormatException {
389
390 int startOffset = offset;
391
392
393 length -= skipWhite(length);
394 if (length == 0) {
395 numberLength = offset - startOffset;
396 return 0;
397 }
398
399 long number = 0;
400 boolean error = true;
401
402 long sign = checkSign();
403 if (foundSign) {
404 length--;
405 }
406
407 while (length > 0 && input[offset] >= '0' && input[offset] <= '9') {
408 number = number * ByteParser.NUMBER_BASE + input[offset] - '0';
409 error = false;
410 offset++;
411 length--;
412 }
413
414 if (error) {
415 numberLength = 0;
416 offset = startOffset;
417 throw new FormatException("Invalid long number");
418 }
419 numberLength = offset - startOffset;
420 return sign * number;
421 }
422
423
424
425
426
427 public int getNumberLength() {
428 return numberLength;
429 }
430
431
432
433
434
435
436 public int getOffset() {
437 return offset;
438 }
439
440
441
442
443
444
445 public String getString(int length) {
446
447 String s = AsciiFuncs.asciiString(input, offset, length);
448 offset += length;
449 numberLength = length;
450 return s;
451 }
452
453 private boolean isCaseInsensitiv(int length, int constantLength, byte[] lowerConstant, byte[] upperConstant) {
454 if (length < constantLength) {
455 return false;
456 }
457 for (int i = 0; i < constantLength; i++) {
458 if (input[offset + i] != lowerConstant[i] && input[offset + i] != upperConstant[i]) {
459 return false;
460 }
461 }
462 return true;
463 }
464
465
466
467
468
469
470 public void setBuffer(byte[] buf) {
471 input = buf;
472 offset = 0;
473 }
474
475
476
477
478
479
480 public void setOffset(int offset) {
481 this.offset = offset;
482 }
483
484
485
486
487
488
489 public void skip(int nBytes) {
490 offset += nBytes;
491 }
492
493
494
495
496
497
498
499
500 public int skipWhite(int length) {
501 int i;
502 for (i = 0; i < length; i++) {
503 if (input[offset + i] != ' ' && input[offset + i] != '\t' && input[offset + i] != '\n'
504 && input[offset + i] != '\r') {
505 break;
506 }
507 }
508 offset += i;
509 return i;
510 }
511 }