00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 package org.openmobileis.common.util.codec;
00030
00039 import java.io.*;
00040 import java.io.CharArrayWriter;
00041
00042 import org.apache.commons.codec.binary.Base64;
00043 import org.apache.commons.codec.net.QuotedPrintableCodec;
00044
00045
00046
00047
00048 public class GeneralCoder {
00049
00050 public GeneralCoder() {
00051 }
00052
00053 public static byte[] decodeBase64(byte[] inByte) throws IOException{
00054
00055 return Base64.decodeBase64(inByte);
00056 }
00057
00058 public static byte[] encodeBase64(byte[] inByte) throws IOException {
00059 return Base64.encodeBase64(inByte);
00060 }
00061
00065 public static String decodeQuotedPrintable(String data) {
00066 try {
00067 return new String(QuotedPrintableCodec.decodeQuotedPrintable(data.getBytes()));
00068 } catch (Exception ex) {
00069 return null;
00070 }
00071 }
00072
00073 public static String encodeQuotedPrintable(String data) {
00074 try {
00075 return new String(QuotedPrintableCodec.encodeQuotedPrintable(null,data.getBytes()));
00076 } catch (Exception ex) {
00077 return null;
00078 }
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00092 public static String decoderfc2047(String data){
00093 try {
00094 char[] encoded = data.toCharArray();
00095 CharArrayWriter out = new CharArrayWriter();
00096 boolean seeequals = false;
00097 int i=0;
00098 while (i<encoded.length) {
00099 if (encoded[i] == '=') {
00100 if ((i<(encoded.length-1)) &&(encoded[i+1] == '?')) {
00101
00102 i+=2;
00103 int step = 0;
00104 StringBuffer tempString = new StringBuffer();
00105 char encoding = 0;
00106 while(i<encoded.length) {
00107 if (encoded[i] == '?') {
00108
00109 switch (step) {
00110 case 0 :
00111
00112 tempString.setLength(0);
00113 break;
00114 case 1 :
00115
00116 encoding = tempString.charAt(0);
00117 tempString.setLength(0);
00118 break;
00119 case 2 :
00120
00121 if (encoding == 'Q') {
00122 String content = GeneralCoder.decodeQuotedPrintable(tempString.toString());
00123 out.write(content.toCharArray());
00124 } else if (encoding == 'B') {
00125 byte[] content = GeneralCoder.decodeBase64(tempString.toString().getBytes());
00126 out.write(new String(content).toCharArray());
00127 }
00128 tempString.setLength(0);
00129
00130 i++;
00131 break;
00132 }
00133 step ++;
00134 i++;
00135 if (step == 3) break;
00136 } else {
00137 tempString.append(encoded[i]);
00138 i++;
00139 }
00140 }
00141 continue;
00142 }
00143 }
00144 out.write(encoded[i]);
00145 i++;
00146 }
00147 out.close();
00148 return out.toString();
00149 }catch (Throwable ex) {
00150 return data;
00151 }
00152 }
00153
00154 public static void main(String[] args) {
00155 String totest[] = {"From: =?ISO-8859-1?Q?Patrik_F=E4ltstr=F6m?= <paf@nada.kth.se>"
00156 , ""
00157 , "normal"
00158 , "avec un ="
00159 , "avec un equals = non fin"
00160 , "=?ISO-8859-1?Q?a"
00161 ,"=?ISO-8859-1?Q?a?= b"
00162 ,"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?="
00163 ,"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?="
00164 ,"=?ISO-8859-1?Q?a_b?="
00165 ,"=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?="
00166
00167 };
00168 try {
00169 for (int i=0; i<totest.length; i++) {
00170 String decode = GeneralCoder.decoderfc2047(totest[i]);
00171 System.out.println(decode);
00172 }
00173 } catch (Throwable ex) {
00174 ex.printStackTrace();
00175 }
00176
00177
00178 }
00179 }