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