GeneralCoder.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
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 // import HTTPClient.Codecs;
00046 // import HTTPClient.NVPair;
00047 
00048 public class GeneralCoder {
00049 
00050   public GeneralCoder() {
00051   }
00052 
00053   public static byte[] decodeBase64(byte[] inByte) throws IOException{
00054                   // decode data
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  /* public static NVPair[] mpFormDataDecode(byte[] data, String cont_type, String dir) {
00082         try {
00083                 return Codecs.mpFormDataDecode(data, cont_type, dir);
00084         } catch (Exception ex) {
00085                 return null;
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] == '=')  { // get first '=?
00099           if ((i<(encoded.length-1)) &&(encoded[i+1] == '?')) {
00100             // begin decode rfc
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                 // manage current tempString
00108                 switch (step) {
00109                   case 0 :
00110                     //does not manage char set (we suppose its ok
00111                     tempString.setLength(0);
00112                     break;
00113                   case 1 :
00114                     //manage encoding
00115                     encoding = tempString.charAt(0);
00116                     tempString.setLength(0);
00117                     break;
00118                   case 2 :
00119                     //decode content
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                     //end of encoded remove final equals and continue string parsing
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; // if an error occurs return original 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" //a
00160       ,"=?ISO-8859-1?Q?a?= b" //a b
00161       ,"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=" //ab
00162       ,"=?ISO-8859-1?Q?a?=  =?ISO-8859-1?Q?b?=" //ab
00163       ,"=?ISO-8859-1?Q?a_b?=" //a b
00164       ,"=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?=" //a 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 }

Generated on Mon Dec 4 11:03:27 2006 for OpenMobileIS by  doxygen 1.5.1-p1