Debian Codegraph @

src/main/java/org/freehep/util/io/ASCIIHexInputStream.java

raw
1 // Copyright 2001, FreeHEP.
2 package org.freehep.util.io;
3
4 import java.io.IOException;
5 import java.io.InputStream;
6
7 /**
8 * The ASCIIHexOutputStream decodes ASCII Hexadecimal. The exact definition of
9 * ASCII Hex encoding can be found in the PostScript Language Reference (3rd
10 * ed.) chapter 3.13.3.
11 *
12 * IMPORTANT: inherits from InputStream rather than FilterInputStream so that
13 * the correct read(byte[], int, int) method is used.
14 *
15 * @author Mark Donszelmann
16 * @version $Id: ASCIIHexInputStream.java 8584 2006-08-10 23:06:37Z duns $
17 */
18 public class ASCIIHexInputStream extends InputStream {
19
20 private boolean ignoreIllegalChars;
21
22 private boolean endReached;
23
24 private int prev;
25
26 private int lineNo;
27
28 private InputStream in;
29
30 /**
31 * Create an ASCIIHex Input Stream from given stream
32 *
33 * @param input input stream to use
34 */
35 public ASCIIHexInputStream(InputStream input) {
36 this(input, false);
37 }
38
39 /**
40 * Create an ASCIIHex Input Stream from given stream and have it optionally
41 * ignore illegal characters
42 *
43 * @param input input stream to use
44 * @param ignore ignore illegal characters
45 */
46 public ASCIIHexInputStream(InputStream input, boolean ignore) {
47 super();
48 in = input;
49 ignoreIllegalChars = ignore;
50 endReached = false;
51 prev = -1;
52 lineNo = 1;
53 }
54
55 public int read() throws IOException {
56 if (endReached)
57 return -1;
58
59 int b0 = readPart();
60 if (b0 == -1)
61 return -1;
62
63 int b1 = readPart();
64 if (b1 == -1)
65 b1 = 0;
66
67 int d = (b0 << 4 | b1) & 0x00FF;
68 return d;
69 }
70
71 /**
72 * @return current line number
73 */
74 public int getLineNo() {
75 return lineNo;
76 }
77
78 private int readPart() throws IOException, EncodingException {
79 while (true) {
80 int b = in.read();
81 switch (b) {
82 case -1:
83 endReached = true;
84 if (!ignoreIllegalChars) {
85 throw new EncodingException(
86 "missing '>' at end of ASCII HEX stream");
87 }
88 return -1;
89 case '>':
90 endReached = true;
91 return -1;
92 case '\r':
93 lineNo++;
94 prev = b;
95 break;
96 case '\n':
97 if (prev != '\r') {
98 lineNo++;
99 }
100 prev = b;
101 break;
102 case ' ':
103 case '\t':
104 case '\f':
105 case 0:
106 // skip whitespace
107 prev = b;
108 break;
109 case '0':
110 return 0;
111 case '1':
112 return 1;
113 case '2':
114 return 2;
115 case '3':
116 return 3;
117 case '4':
118 return 4;
119 case '5':
120 return 5;
121 case '6':
122 return 6;
123 case '7':
124 return 7;
125 case '8':
126 return 8;
127 case '9':
128 return 9;
129 case 'A':
130 case 'a':
131 return 10;
132 case 'B':
133 case 'b':
134 return 11;
135 case 'C':
136 case 'c':
137 return 12;
138 case 'D':
139 case 'd':
140 return 13;
141 case 'E':
142 case 'e':
143 return 14;
144 case 'F':
145 case 'f':
146 return 15;
147 default:
148 if (!ignoreIllegalChars) {
149 throw new EncodingException("Illegal char " + b
150 + " in HexStream");
151 }
152 prev = b;
153 break;
154 }
155 }
156 }
157 }