KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > RFC2047


1 /*
2  * RFC2047.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: RFC2047.java,v 1.1.1.1 2002/09/24 16:10:04 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import gnu.regexp.RE;
25 import gnu.regexp.REMatch;
26 import gnu.regexp.UncheckedRE;
27 import java.io.UnsupportedEncodingException JavaDoc;
28 import org.armedbear.j.FastStringBuffer;
29 import org.armedbear.j.Log;
30 import org.armedbear.j.Utilities;
31
32 public final class RFC2047
33 {
34     private static final RE prefixRE = new UncheckedRE("=\\?[^?]+\\?[bq]\\?");
35
36     public static String JavaDoc decode(String JavaDoc encoded)
37     {
38         if (encoded == null)
39             return null;
40         // Fail fast.
41
if (encoded.indexOf("=?") < 0)
42             return encoded;
43         REMatch match = prefixRE.getMatch(encoded.toLowerCase());
44         if (match == null) {
45             Log.error("RFC2047.decode prefix is null");
46             Log.error("encoded = |" + encoded + "|");
47             return encoded;
48         }
49         String JavaDoc prefix = match.toString();
50         int index = match.getStartIndex();
51         String JavaDoc charset = prefix.substring(2, prefix.length()-3);
52         String JavaDoc encoding = Utilities.getEncodingFromCharset(charset);
53         FastStringBuffer sb = new FastStringBuffer();
54         sb.append(encoded.substring(0, index));
55         String JavaDoc remaining = encoded.substring(index);
56         while (true) {
57             int begin = prefix.length();
58             int end = remaining.indexOf("?=", begin);
59             if (end < 0) {
60                 // Error.
61
sb.append(remaining);
62                 Log.error("RFC2047.decode error");
63                 Log.error("encoded = |" + encoded + "|");
64                 Log.error("remaining = |" + remaining + "|");
65                 return sb.toString();
66             }
67             byte[] bytes = null;
68             if (prefix.endsWith("?b?"))
69                 bytes = Base64Decoder.decode(remaining.substring(begin, end));
70             else if (prefix.endsWith("?q?"))
71                 bytes = QuotedPrintableDecoder.decode(remaining.substring(begin, end));
72             if (bytes == null) {
73                 Log.error("RFC2047.decode error");
74                 Log.error("encoded = |" + encoded + "|");
75                 return encoded;
76             }
77             try {
78                 sb.append(new String JavaDoc(bytes, encoding));
79             }
80             catch (UnsupportedEncodingException JavaDoc e) {
81                 Log.error(e);
82                 return encoded;
83             }
84             remaining = remaining.substring(end+2);
85             index = remaining.toLowerCase().indexOf(prefix);
86             if (index < 0) {
87                 sb.append(remaining);
88                 break;
89             }
90             sb.append(remaining.substring(0, index));
91             remaining = remaining.substring(index);
92         }
93         return sb.toString();
94     }
95 }
96
Popular Tags