風行天下

          JAVA太極
          posts - 4, comments - 10, trackbacks - 0, articles - 55
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Jakarta Commons Codec

          Posted on 2005-04-04 11:21 風太少 閱讀(450) 評論(0)  編輯  收藏
          SECTION 01 Codec 總覽

          Java JDK有一個 java.security 的 package, 提供了 MessageDigest 的編碼方式, Digest Algorithms 包括了 MD2, MD5, SHA-1, SHA-256, SHA-384, 及 SHA-512 等等. 但是我們常會需要用到其它的編碼演算, 此時的解決方法, 可以先來看看 commons codec 是否已經加入標準, 不然就去一些學術單位尋找, 真的沒有, 就只好自己寫了.

          目前 commons codec 已經有提供 base64, hex, 及 metaphone, soundex 等編碼. 下載位置為 codec 1.1


          SECTION 02 Base64 編解碼

          base64 編解碼技術廣泛地利用在 Internet 之上, 主要就是將 bytecode 轉換成 ascii 碼, 最常常使用的則是屬于電子郵件附件的傳輸. 你可以查看你的郵件原碼, 如果有 Content-Transfer-Encoding: base64 , 就代表這封信有采用 base64 來編碼, 另外則是 QP ( quoted-printable ) 編碼方式, 基本上大多數的讀信軟件都有支持.
          
          package com.softleader.sample.codec;
          
          import org.apache.commons.codec.binary.Base64;
          import org.apache.commons.codec.*;
          
          public class Base64Test {
          
              public static void main(String args[]) {
                  Base64 base64 = new Base64();
                  String str = "中文";
                  byte[] enbytes = null;
                  String encodeStr = null;
                  byte[] debytes = null;
                  String decodeStr = null;
                  try {
                      enbytes = base64.encode(str.getBytes());
                      encodeStr = new String(enbytes);
                      debytes = base64.decode(enbytes);
                      decodeStr = new String(debytes);
                  } catch (EncoderException ex) {
                      System.out.println("編碼錯誤");
                  } catch (DecoderException ex) {
                      System.out.println("解碼錯誤");
                  }
                  System.out.println("編碼前:"+str);
                  System.out.println("編碼后:"+encodeStr);
                  System.out.println("解碼后:"+decodeStr);
              }
          }
          
          cmd>java com.softleader.sample.codec.Base64Test
          執行結果
          編碼前:中文
          編碼后:pKSk5Q==
          解碼后:中文



          SECTION 03 Hex 編解碼

          通常我們會對于 URL Form GET 時候進行 16 進位編碼, 將 byte[] 轉成 char[],
          
          package com.softleader.sample.codec;
          
          import org.apache.commons.codec.binary.Hex;
          import org.apache.commons.codec.*;
          
          public class HexTest {
          
              public static void main(String args[]) {
                  Hex hex = new Hex();
                  String str = "中文";
                  char[] enbytes = null;
                  String encodeStr = null;
                  byte[] debytes = null;
                  String decodeStr = null;
                  try {
                      enbytes = hex.encodeHex(str.getBytes());
                      encodeStr = new String(enbytes);
                      debytes = hex.decodeHex(enbytes);
                      decodeStr = new String(debytes);
                  } catch (Exception ex) {
                      ;
                  }
                  System.out.println("編碼前:"+str);
                  System.out.println("編碼后:"+encodeStr);
                  System.out.println("解碼后:"+decodeStr);
              }
          }
          
          cmd>java com.softleader.sample.codec.HexTest
          執行結果
          編碼前:中文
          編碼后:a4a4a4e5
          解碼后:中文



          SECTION 04 Metaphone 及 Soundex 編碼

          Metaphone 建立出相同的key給發音相似的單字, 比 Soundex 還要準確, 但是 Metaphone 沒有固定長度, Soundex 則是固定第一個英文字加上3個數字. 這通常是用在類似音比對, 也可以用在 MP3 的軟件開發.
          
          package com.softleader.sample.codec;
          
          import org.apache.commons.codec.language.*;
          import org.apache.commons.codec.*;
          
          public class LanguageTest {
          
              public static void main(String args[]) {
                  Metaphone metaphone = new Metaphone();
                  RefinedSoundex refinedSoundex = new RefinedSoundex();
                  Soundex soundex = new Soundex();
          
                  for (int i=0; i<2; i++ ) {
                      String str=(i==0)?"resume":"resin";
          
                      String mString = null;
                      String rString = null;
                      String sString = null;
          
                      try {
                          mString = metaphone.encode(str);
                          rString = refinedSoundex.encode(str);
                          sString = soundex.encode(str);
          
                      } catch (Exception ex) {
                          ;
                      }
                      System.out.println("Original:"+str);
                      System.out.println("Metaphone:"+mString);
                      System.out.println("RefinedSoundex:"+rString);
                      System.out.println("Soundex:"+sString +"\n");
          
                  }
              }
          }
          
          
          cmd>java com.softleader.sample.codec.LanguageTest
          執行結果
          Original:resume
          Metaphone:RSM
          RefinedSoundex:R903080
          Soundex:R250


          Original:resin
          Metaphone:RSN
          RefinedSoundex:R90308
          Soundex:R250


          可以看到 Soundex 靈敏度較低

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 新闻| 平陆县| 密山市| 綦江县| 莒南县| 松阳县| 泸州市| 宁夏| 鄂州市| 望都县| 玉环县| 营山县| 东乡族自治县| 凤山县| 珲春市| 信阳市| 桃源县| 会宁县| 弋阳县| 横峰县| 通化市| 曲沃县| 西贡区| 和政县| 沙洋县| 汾阳市| 井研县| 农安县| 拜泉县| 南和县| 荥经县| 阿荣旗| 郧西县| 白水县| 衢州市| 濮阳市| 易门县| 通河县| 濉溪县| 乐陵市| 云梦县|