1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| package org.apache.geronimo.mail.util; |
19 |
| |
20 |
| import java.io.IOException; |
21 |
| import java.io.OutputStream; |
22 |
| |
23 |
| public class HexEncoder |
24 |
| implements Encoder |
25 |
| { |
26 |
| protected final byte[] encodingTable = |
27 |
| { |
28 |
| (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', |
29 |
| (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f' |
30 |
| }; |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| protected final byte[] decodingTable = new byte[128]; |
36 |
| |
37 |
0
| protected void initialiseDecodingTable()
|
38 |
| { |
39 |
0
| for (int i = 0; i < encodingTable.length; i++)
|
40 |
| { |
41 |
0
| decodingTable[encodingTable[i]] = (byte)i;
|
42 |
| } |
43 |
| |
44 |
0
| decodingTable['A'] = decodingTable['a'];
|
45 |
0
| decodingTable['B'] = decodingTable['b'];
|
46 |
0
| decodingTable['C'] = decodingTable['c'];
|
47 |
0
| decodingTable['D'] = decodingTable['d'];
|
48 |
0
| decodingTable['E'] = decodingTable['e'];
|
49 |
0
| decodingTable['F'] = decodingTable['f'];
|
50 |
| } |
51 |
| |
52 |
0
| public HexEncoder()
|
53 |
| { |
54 |
0
| initialiseDecodingTable();
|
55 |
| } |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
0
| public int encode(
|
63 |
| byte[] data, |
64 |
| int off, |
65 |
| int length, |
66 |
| OutputStream out) |
67 |
| throws IOException |
68 |
| { |
69 |
0
| for (int i = off; i < (off + length); i++)
|
70 |
| { |
71 |
0
| int v = data[i] & 0xff;
|
72 |
| |
73 |
0
| out.write(encodingTable[(v >>> 4)]);
|
74 |
0
| out.write(encodingTable[v & 0xf]);
|
75 |
| } |
76 |
| |
77 |
0
| return length * 2;
|
78 |
| } |
79 |
| |
80 |
0
| private boolean ignore(
|
81 |
| char c) |
82 |
| { |
83 |
0
| return (c == '\n' || c =='\r' || c == '\t' || c == ' ');
|
84 |
| } |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
0
| public int decode(
|
93 |
| byte[] data, |
94 |
| int off, |
95 |
| int length, |
96 |
| OutputStream out) |
97 |
| throws IOException |
98 |
| { |
99 |
0
| byte[] bytes;
|
100 |
0
| byte b1, b2;
|
101 |
0
| int outLen = 0;
|
102 |
| |
103 |
0
| int end = off + length;
|
104 |
| |
105 |
0
| while (end > 0)
|
106 |
| { |
107 |
0
| if (!ignore((char)data[end - 1]))
|
108 |
| { |
109 |
0
| break;
|
110 |
| } |
111 |
| |
112 |
0
| end--;
|
113 |
| } |
114 |
| |
115 |
0
| int i = off;
|
116 |
0
| while (i < end)
|
117 |
| { |
118 |
0
| while (i < end && ignore((char)data[i]))
|
119 |
| { |
120 |
0
| i++;
|
121 |
| } |
122 |
| |
123 |
0
| b1 = decodingTable[data[i++]];
|
124 |
| |
125 |
0
| while (i < end && ignore((char)data[i]))
|
126 |
| { |
127 |
0
| i++;
|
128 |
| } |
129 |
| |
130 |
0
| b2 = decodingTable[data[i++]];
|
131 |
| |
132 |
0
| out.write((b1 << 4) | b2);
|
133 |
| |
134 |
0
| outLen++;
|
135 |
| } |
136 |
| |
137 |
0
| return outLen;
|
138 |
| } |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
0
| public int decode(
|
147 |
| String data, |
148 |
| OutputStream out) |
149 |
| throws IOException |
150 |
| { |
151 |
0
| byte[] bytes;
|
152 |
0
| byte b1, b2, b3, b4;
|
153 |
0
| int length = 0;
|
154 |
| |
155 |
0
| int end = data.length();
|
156 |
| |
157 |
0
| while (end > 0)
|
158 |
| { |
159 |
0
| if (!ignore(data.charAt(end - 1)))
|
160 |
| { |
161 |
0
| break;
|
162 |
| } |
163 |
| |
164 |
0
| end--;
|
165 |
| } |
166 |
| |
167 |
0
| int i = 0;
|
168 |
0
| while (i < end)
|
169 |
| { |
170 |
0
| while (i < end && ignore(data.charAt(i)))
|
171 |
| { |
172 |
0
| i++;
|
173 |
| } |
174 |
| |
175 |
0
| b1 = decodingTable[data.charAt(i++)];
|
176 |
| |
177 |
0
| while (i < end && ignore(data.charAt(i)))
|
178 |
| { |
179 |
0
| i++;
|
180 |
| } |
181 |
| |
182 |
0
| b2 = decodingTable[data.charAt(i++)];
|
183 |
| |
184 |
0
| out.write((b1 << 4) | b2);
|
185 |
| |
186 |
0
| length++;
|
187 |
| } |
188 |
| |
189 |
0
| return length;
|
190 |
| } |
191 |
| } |