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 XTextEncoder |
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 |
| |
45 |
0
| public XTextEncoder()
|
46 |
| { |
47 |
0
| initialiseDecodingTable();
|
48 |
| } |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
0
| public int encode(
|
56 |
| byte[] data, |
57 |
| int off, |
58 |
| int length, |
59 |
| OutputStream out) |
60 |
| throws IOException |
61 |
| { |
62 |
0
| int bytesWritten = 0;
|
63 |
| |
64 |
0
| for (int i = off; i < (off + length); i++)
|
65 |
| { |
66 |
0
| int v = data[i] & 0xff;
|
67 |
| |
68 |
0
| if (v < 33 || v > 126 || v == '+' || v == '+') {
|
69 |
0
| out.write((byte)'+');
|
70 |
0
| out.write(encodingTable[(v >>> 4)]);
|
71 |
0
| out.write(encodingTable[v & 0xf]);
|
72 |
0
| bytesWritten += 3;
|
73 |
| } |
74 |
| else { |
75 |
| |
76 |
0
| out.write((byte)v);
|
77 |
0
| bytesWritten++;
|
78 |
| } |
79 |
| } |
80 |
| |
81 |
0
| return bytesWritten;
|
82 |
| } |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
0
| public int decode(
|
91 |
| byte[] data, |
92 |
| int off, |
93 |
| int length, |
94 |
| OutputStream out) |
95 |
| throws IOException |
96 |
| { |
97 |
0
| byte[] bytes;
|
98 |
0
| byte b1, b2;
|
99 |
0
| int outLen = 0;
|
100 |
| |
101 |
0
| int end = off + length;
|
102 |
| |
103 |
0
| int i = off;
|
104 |
0
| while (i < end)
|
105 |
| { |
106 |
0
| byte v = data[i++];
|
107 |
| |
108 |
0
| if (v == '+') {
|
109 |
0
| b1 = decodingTable[data[i++]];
|
110 |
0
| b2 = decodingTable[data[i++]];
|
111 |
0
| out.write((b1 << 4) | b2);
|
112 |
| } |
113 |
| else { |
114 |
| |
115 |
0
| out.write(v);
|
116 |
| } |
117 |
| |
118 |
0
| outLen++;
|
119 |
| } |
120 |
| |
121 |
0
| return outLen;
|
122 |
| } |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
0
| public int decode(
|
130 |
| String data, |
131 |
| OutputStream out) |
132 |
| throws IOException |
133 |
| { |
134 |
0
| byte[] bytes;
|
135 |
0
| byte b1, b2, b3, b4;
|
136 |
0
| int length = 0;
|
137 |
| |
138 |
0
| int end = data.length();
|
139 |
| |
140 |
0
| int i = 0;
|
141 |
0
| while (i < end)
|
142 |
| { |
143 |
0
| char v = data.charAt(i++);
|
144 |
0
| if (v == '+') {
|
145 |
0
| b1 = decodingTable[data.charAt(i++)];
|
146 |
0
| b2 = decodingTable[data.charAt(i++)];
|
147 |
| |
148 |
0
| out.write((b1 << 4) | b2);
|
149 |
| } |
150 |
| else { |
151 |
0
| out.write((byte)v);
|
152 |
| } |
153 |
0
| length++;
|
154 |
| } |
155 |
| |
156 |
0
| return length;
|
157 |
| } |
158 |
| } |
159 |
| |