| 
 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.ByteArrayOutputStream; | 
| 
 21 | 
  
 | import java.io.IOException; | 
| 
 22 | 
  
 | import java.io.OutputStream; | 
| 
 23 | 
  
 | import java.io.UnsupportedEncodingException; | 
| 
 24 | 
  
 |  | 
| 
 25 | 
  
 | import javax.mail.internet.MimeUtility; | 
| 
 26 | 
  
 |  | 
| 
 27 | 
  
 |  | 
| 
 28 | 
  
 |  | 
| 
 29 | 
  
 |  | 
| 
 30 | 
  
 |  | 
| 
 31 | 
  
 |  | 
| 
 32 | 
  
 |  | 
| 
 33 | 
  
 |  | 
| 
 34 | 
  
 |  | 
| 
 35 | 
  
 |  | 
| 
 36 | 
  
 |  | 
| 
 37 | 
  
 |  | 
| 
 38 | 
  
 |  | 
| 
 39 | 
  
 |  | 
| 
 40 | 
  
 |  | 
| 
 41 | 
  
 |  | 
| 
 42 | 
  
 |  | 
| 
 43 | 
  
 |  | 
| 
 44 | 
  
 |  | 
| 
 45 | 
  
 |  | 
| 
 46 | 
  
 |  | 
| 
 47 | 
  
 | public class RFC2231Encoder implements Encoder | 
| 
 48 | 
  
 | { | 
| 
 49 | 
  
 |     protected final byte[] encodingTable = | 
| 
 50 | 
  
 |         { | 
| 
 51 | 
  
 |             (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', | 
| 
 52 | 
  
 |             (byte)'8', (byte)'9', (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F' | 
| 
 53 | 
  
 |         }; | 
| 
 54 | 
  
 |  | 
| 
 55 | 
  
 |     protected String DEFAULT_SPECIALS = " *'%"; | 
| 
 56 | 
  
 |     protected String specials = DEFAULT_SPECIALS; | 
| 
 57 | 
  
 |  | 
| 
 58 | 
  
 |      | 
| 
 59 | 
  
 |  | 
| 
 60 | 
  
 |  | 
| 
 61 | 
  
 |     protected final byte[] decodingTable = new byte[128]; | 
| 
 62 | 
  
 |  | 
| 
 63 | 
 0
 |     protected void initialiseDecodingTable()
 | 
| 
 64 | 
  
 |     { | 
| 
 65 | 
 0
 |         for (int i = 0; i < encodingTable.length; i++)
 | 
| 
 66 | 
  
 |         { | 
| 
 67 | 
 0
 |             decodingTable[encodingTable[i]] = (byte)i;
 | 
| 
 68 | 
  
 |         } | 
| 
 69 | 
  
 |     } | 
| 
 70 | 
  
 |  | 
| 
 71 | 
 0
 |     public RFC2231Encoder()
 | 
| 
 72 | 
  
 |     { | 
| 
 73 | 
 0
 |         this(null);
 | 
| 
 74 | 
  
 |     } | 
| 
 75 | 
  
 |  | 
| 
 76 | 
 0
 |     public RFC2231Encoder(String specials)
 | 
| 
 77 | 
  
 |     { | 
| 
 78 | 
 0
 |         if (specials != null) {
 | 
| 
 79 | 
 0
 |             this.specials = DEFAULT_SPECIALS + specials;
 | 
| 
 80 | 
  
 |         } | 
| 
 81 | 
 0
 |         initialiseDecodingTable();
 | 
| 
 82 | 
  
 |     } | 
| 
 83 | 
  
 |  | 
| 
 84 | 
  
 |  | 
| 
 85 | 
  
 |      | 
| 
 86 | 
  
 |  | 
| 
 87 | 
  
 |  | 
| 
 88 | 
  
 |  | 
| 
 89 | 
  
 |  | 
| 
 90 | 
 0
 |     public int encode(byte[] data, int off, int length, OutputStream out) throws IOException {
 | 
| 
 91 | 
  
 |  | 
| 
 92 | 
 0
 |         int bytesWritten = 0;
 | 
| 
 93 | 
 0
 |         for (int i = off; i < (off + length); i++)
 | 
| 
 94 | 
  
 |         { | 
| 
 95 | 
 0
 |             int ch = data[i] & 0xff;
 | 
| 
 96 | 
  
 |              | 
| 
 97 | 
 0
 |             if (ch <= 32 || ch >= 127 || specials.indexOf(ch) != -1) {
 | 
| 
 98 | 
 0
 |                 out.write((byte)'%');
 | 
| 
 99 | 
 0
 |                 out.write(encodingTable[ch >> 4]);
 | 
| 
 100 | 
 0
 |                 out.write(encodingTable[ch & 0xf]);
 | 
| 
 101 | 
 0
 |                 bytesWritten += 3;
 | 
| 
 102 | 
  
 |             } | 
| 
 103 | 
  
 |             else { | 
| 
 104 | 
  
 |                  | 
| 
 105 | 
 0
 |                 out.write((byte)ch);
 | 
| 
 106 | 
 0
 |                 bytesWritten++;
 | 
| 
 107 | 
  
 |             } | 
| 
 108 | 
  
 |         } | 
| 
 109 | 
  
 |  | 
| 
 110 | 
 0
 |         return bytesWritten;
 | 
| 
 111 | 
  
 |     } | 
| 
 112 | 
  
 |  | 
| 
 113 | 
  
 |  | 
| 
 114 | 
  
 |      | 
| 
 115 | 
  
 |  | 
| 
 116 | 
  
 |  | 
| 
 117 | 
  
 |  | 
| 
 118 | 
  
 |  | 
| 
 119 | 
 0
 |     public int decode(byte[] data, int off, int length, OutputStream out) throws IOException {
 | 
| 
 120 | 
 0
 |         int        outLen = 0;
 | 
| 
 121 | 
 0
 |         int        end = off + length;
 | 
| 
 122 | 
  
 |  | 
| 
 123 | 
 0
 |         int i = off;
 | 
| 
 124 | 
 0
 |         while (i < end)
 | 
| 
 125 | 
  
 |         { | 
| 
 126 | 
 0
 |             byte v = data[i++];
 | 
| 
 127 | 
  
 |              | 
| 
 128 | 
 0
 |             if (v == '%') {
 | 
| 
 129 | 
 0
 |                 byte b1 = decodingTable[data[i++]];
 | 
| 
 130 | 
 0
 |                 byte b2 = decodingTable[data[i++]];
 | 
| 
 131 | 
 0
 |                 out.write((b1 << 4) | b2);
 | 
| 
 132 | 
  
 |             } | 
| 
 133 | 
  
 |             else { | 
| 
 134 | 
  
 |                  | 
| 
 135 | 
 0
 |                 out.write(v);
 | 
| 
 136 | 
  
 |             } | 
| 
 137 | 
  
 |              | 
| 
 138 | 
 0
 |             outLen++;
 | 
| 
 139 | 
  
 |         } | 
| 
 140 | 
  
 |  | 
| 
 141 | 
 0
 |         return outLen;
 | 
| 
 142 | 
  
 |     } | 
| 
 143 | 
  
 |  | 
| 
 144 | 
  
 |      | 
| 
 145 | 
  
 |  | 
| 
 146 | 
  
 |  | 
| 
 147 | 
  
 |  | 
| 
 148 | 
  
 |  | 
| 
 149 | 
 0
 |     public int decode(String data, OutputStream out) throws IOException
 | 
| 
 150 | 
  
 |     { | 
| 
 151 | 
 0
 |         int        length = 0;
 | 
| 
 152 | 
 0
 |         int        end = data.length();
 | 
| 
 153 | 
  
 |  | 
| 
 154 | 
 0
 |         int i = 0;
 | 
| 
 155 | 
 0
 |         while (i < end)
 | 
| 
 156 | 
  
 |         { | 
| 
 157 | 
 0
 |             char v = data.charAt(i++);
 | 
| 
 158 | 
 0
 |             if (v == '%') {
 | 
| 
 159 | 
 0
 |                 byte b1 = decodingTable[data.charAt(i++)];
 | 
| 
 160 | 
 0
 |                 byte b2 = decodingTable[data.charAt(i++)];
 | 
| 
 161 | 
  
 |  | 
| 
 162 | 
 0
 |                 out.write((b1 << 4) | b2);
 | 
| 
 163 | 
  
 |             } | 
| 
 164 | 
  
 |             else { | 
| 
 165 | 
 0
 |                 out.write((byte)v);
 | 
| 
 166 | 
  
 |             } | 
| 
 167 | 
 0
 |             length++;
 | 
| 
 168 | 
  
 |         } | 
| 
 169 | 
  
 |  | 
| 
 170 | 
 0
 |         return length;
 | 
| 
 171 | 
  
 |     } | 
| 
 172 | 
  
 |  | 
| 
 173 | 
  
 |  | 
| 
 174 | 
  
 |      | 
| 
 175 | 
  
 |  | 
| 
 176 | 
  
 |  | 
| 
 177 | 
  
 |  | 
| 
 178 | 
  
 |  | 
| 
 179 | 
  
 |  | 
| 
 180 | 
  
 |  | 
| 
 181 | 
  
 |  | 
| 
 182 | 
  
 |  | 
| 
 183 | 
  
 |  | 
| 
 184 | 
 0
 |     public String encode(String charset, String language, String data) throws IOException {
 | 
| 
 185 | 
  
 |  | 
| 
 186 | 
 0
 |         byte[] bytes = null;
 | 
| 
 187 | 
 0
 |         try {
 | 
| 
 188 | 
  
 |              | 
| 
 189 | 
  
 |              | 
| 
 190 | 
 0
 |             bytes = data.getBytes(MimeUtility.javaCharset(charset));
 | 
| 
 191 | 
  
 |         } catch (UnsupportedEncodingException e) { | 
| 
 192 | 
  
 |              | 
| 
 193 | 
 0
 |             return null;
 | 
| 
 194 | 
  
 |         } | 
| 
 195 | 
  
 |  | 
| 
 196 | 
 0
 |         StringBuffer result = new StringBuffer();
 | 
| 
 197 | 
  
 |  | 
| 
 198 | 
  
 |          | 
| 
 199 | 
 0
 |         if (charset != null) {
 | 
| 
 200 | 
 0
 |             result.append(charset);
 | 
| 
 201 | 
  
 |         } | 
| 
 202 | 
  
 |          | 
| 
 203 | 
 0
 |         result.append("'");
 | 
| 
 204 | 
  
 |  | 
| 
 205 | 
  
 |          | 
| 
 206 | 
 0
 |         if (language != null) {
 | 
| 
 207 | 
 0
 |             result.append(language);
 | 
| 
 208 | 
  
 |         } | 
| 
 209 | 
  
 |          | 
| 
 210 | 
 0
 |         result.append("'");
 | 
| 
 211 | 
  
 |  | 
| 
 212 | 
  
 |          | 
| 
 213 | 
 0
 |         OutputStream out = new StringBufferOutputStream(result);
 | 
| 
 214 | 
  
 |  | 
| 
 215 | 
  
 |          | 
| 
 216 | 
 0
 |         encode(bytes, 0, bytes.length, out);
 | 
| 
 217 | 
  
 |  | 
| 
 218 | 
  
 |          | 
| 
 219 | 
 0
 |         return result.toString();
 | 
| 
 220 | 
  
 |     } | 
| 
 221 | 
  
 |  | 
| 
 222 | 
  
 |  | 
| 
 223 | 
  
 |      | 
| 
 224 | 
  
 |  | 
| 
 225 | 
  
 |  | 
| 
 226 | 
  
 |  | 
| 
 227 | 
  
 |  | 
| 
 228 | 
  
 |  | 
| 
 229 | 
  
 |  | 
| 
 230 | 
  
 |  | 
| 
 231 | 
  
 |  | 
| 
 232 | 
 0
 |     public String decode(String data) throws IOException, UnsupportedEncodingException {
 | 
| 
 233 | 
  
 |          | 
| 
 234 | 
 0
 |         int charsetEnd = data.indexOf('\'');
 | 
| 
 235 | 
  
 |          | 
| 
 236 | 
 0
 |         if (charsetEnd == -1) {
 | 
| 
 237 | 
 0
 |             throw new IOException("Missing charset in RFC2231 encoded value");
 | 
| 
 238 | 
  
 |         } | 
| 
 239 | 
  
 |  | 
| 
 240 | 
 0
 |         String charset = data.substring(0, charsetEnd);
 | 
| 
 241 | 
  
 |  | 
| 
 242 | 
  
 |          | 
| 
 243 | 
 0
 |         int languageEnd = data.indexOf('\'', charsetEnd + 1);
 | 
| 
 244 | 
 0
 |         if (languageEnd == -1) {
 | 
| 
 245 | 
 0
 |             throw new IOException("Missing language in RFC2231 encoded value");
 | 
| 
 246 | 
  
 |         } | 
| 
 247 | 
  
 |  | 
| 
 248 | 
 0
 |         String language = data.substring(charsetEnd + 1, languageEnd);
 | 
| 
 249 | 
  
 |  | 
| 
 250 | 
 0
 |         ByteArrayOutputStream out = new ByteArrayOutputStream(data.length());
 | 
| 
 251 | 
  
 |  | 
| 
 252 | 
  
 |          | 
| 
 253 | 
 0
 |         decode(data.substring(languageEnd + 1), out);
 | 
| 
 254 | 
  
 |  | 
| 
 255 | 
 0
 |         byte[] bytes = out.toByteArray();
 | 
| 
 256 | 
  
 |          | 
| 
 257 | 
 0
 |         return new String(bytes, 0, bytes.length, MimeUtility.javaCharset(charset));
 | 
| 
 258 | 
  
 |     } | 
| 
 259 | 
  
 | } |