Jakarta Commons Codec

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

你可能感兴趣的:(commons)