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 int i=0;
00097 while (i<encoded.length) {
00098 if (encoded[i] == '=') {
00099 if ((i<(encoded.length-1)) &&(encoded[i+1] == '?')) {
00100
00101 i+=2;
00102 int step = 0;
00103 StringBuffer tempString = new StringBuffer();
00104 char encoding = 0;
00105 while(i<encoded.length) {
00106 if (encoded[i] == '?') {
00107
00108 switch (step) {
00109 case 0 :
00110
00111 tempString.setLength(0);
00112 break;
00113 case 1 :
00114
00115 encoding = tempString.charAt(0);
00116 tempString.setLength(0);
00117 break;
00118 case 2 :
00119
00120 if (encoding == 'Q') {
00121 String content = GeneralCoder.decodeQuotedPrintable(tempString.toString());
00122 out.write(content.toCharArray());
00123 } else if (encoding == 'B') {
00124 byte[] content = GeneralCoder.decodeBase64(tempString.toString().getBytes());
00125 out.write(new String(content).toCharArray());
00126 }
00127 tempString.setLength(0);
00128
00129 i++;
00130 break;
00131 }
00132 step ++;
00133 i++;
00134 if (step == 3) break;
00135 } else {
00136 tempString.append(encoded[i]);
00137 i++;
00138 }
00139 }
00140 continue;
00141 }
00142 }
00143 out.write(encoded[i]);
00144 i++;
00145 }
00146 out.close();
00147 return out.toString();
00148 }catch (Throwable ex) {
00149 return data;
00150 }
00151 }
00152
00153 public static void main(String[] args) {
00154 String totest[] = {"From: =?ISO-8859-1?Q?Patrik_F=E4ltstr=F6m?= <paf@nada.kth.se>"
00155 , ""
00156 , "normal"
00157 , "avec un ="
00158 , "avec un equals = non fin"
00159 , "=?ISO-8859-1?Q?a"
00160 ,"=?ISO-8859-1?Q?a?= b"
00161 ,"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?="
00162 ,"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?="
00163 ,"=?ISO-8859-1?Q?a_b?="
00164 ,"=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?="
00165
00166 };
00167 try {
00168 for (int i=0; i<totest.length; i++) {
00169 String decode = GeneralCoder.decoderfc2047(totest[i]);
00170 System.out.println(decode);
00171 }
00172 } catch (Throwable ex) {
00173 ex.printStackTrace();
00174 }
00175
00176
00177 }
00178 }