Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

GeneralCoder.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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       boolean seeequals = false;
00097       int i=0;
00098       while (i<encoded.length)  {
00099         if (encoded[i] == '=')  { // get first '=?
00100           if ((i<(encoded.length-1)) &&(encoded[i+1] == '?')) {
00101             // begin decode rfc
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                 // manage current tempString
00109                 switch (step) {
00110                   case 0 :
00111                     //does not manage char set (we suppose its ok
00112                     tempString.setLength(0);
00113                     break;
00114                   case 1 :
00115                     //manage encoding
00116                     encoding = tempString.charAt(0);
00117                     tempString.setLength(0);
00118                     break;
00119                   case 2 :
00120                     //decode content
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                     //end of encoded remove final equals and continue string parsing
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; // if an error occurs return original 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" //a
00161       ,"=?ISO-8859-1?Q?a?= b" //a b
00162       ,"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=" //ab
00163       ,"=?ISO-8859-1?Q?a?=  =?ISO-8859-1?Q?b?=" //ab
00164       ,"=?ISO-8859-1?Q?a_b?=" //a b
00165       ,"=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?=" //a 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 }

Generated on Thu Oct 6 10:06:33 2005 for OpenMobileIS by  doxygen 1.4.3