Java小强个人技术博客站点    手机版
当前位置: 首页 >> 开源 >> Apache Commons Codec

Apache Commons Codec

181120 开源 | 2015-5-6

Apache Commons Codec

Apache Commons Codec (TM) software provides implementations of common encoders and decoders such as Base64, Hex, Phonetic and URLs.

 

Impetus

Codec was formed as an attempt to focus development effort on one definitive implementation of the Base64 encoder. At the time of Codec's proposal, there were approximately 34 different Java classes that dealt with Base64 encoding spread over the Foundation's CVS repository. Developers in the Jakarta Tomcat project had implemented an original version of the Base64 codec which had been copied by the Commons HttpClient and Apache XML project's XML-RPC subproject. After almost one year, the two forked versions of Base64 had significantly diverged from one another. XML-RPC had applied numerous fixes and patches which were not applied to the Commons HttpClient Base64. Different subprojects had differing implementations at various levels of compliance with the RFC 2045.

Out of that confusing duplication of effort sprang this simple attempt to encourage code reuse among various projects. While this package contains a abstract framework for the creation of encoders and decoders, Codec itself is primarily focused on providing functional utilities for working with common encodings.

 

URL:http://commons.apache.org/proper/commons-codec/index.html

 

DEMO:

package test;
import org.apache.commons.codec.binary.Base32;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.BinaryCodec;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.net.URLCodec;
public class CodecTest {
 
 public static void main(String[] args) throws Exception{
  String str = "abc";  
  
  // MD5
  System.out.println(DigestUtils.md5Hex(str)); 
  
  // SHA1
  System.out.println(DigestUtils.sha1Hex(str));
  
  // Base64 加密
  byte[] b = str.getBytes();
  b = Base64.encodeBase64(b);
  System.out.println("Base64 Encode:" + new String(b));
  // Base64 解密
  b = Base64.decodeBase64(b);
  System.out.println("Base64 Decode:" + new String(b));
  
  Base32 b32 = new Base32();
  b = b32.encode(str.getBytes());
  System.out.println("Base32 Encode:" + new String(b));
  b = b32.decode(b);
  System.out.println("Base32 Decode:" + new String(b));
  
  // URL Encode
  URLCodec uc = new URLCodec();
  String s = uc.encode("http://www.javacui.com?name=崔素强", "UTF-8");
  System.out.println("URLCodec encode:" + s);
  s = uc.decode(s);
  System.out.println("URLCodec decode:" + s);
  
  // 二进制
  char[] c = BinaryCodec.toAsciiChars(str.getBytes());
  for(char bt : c){
   System.out.print(bt + " ");
  }
  System.out.println();
  b = BinaryCodec.fromAscii(c);
  System.out.println("BinaryCodec decode:" + new String(b));
  
  // StringUtils
 }
}

 

Console Out:

900150983cd24fb0d6963f7d28e17f72
a9993e364706816aba3e25717850c26c9cd0d89d
Base64 Encode:YWJj
Base64 Decode:abc
Base32 Encode:MFRGG===
Base32 Decode:abc
URLCodec encode:http%3A%2F%2Fwww.javacui.com%3Fname%3D%E5%B4%94%E7%B4%A0%E5%BC%BA
URLCodec decode:http://www.javacui.com?name=崔素强
0 1 1 0 0 0 1 1 0 1 1 0 0 0 1 0 0 1 1 0 0 0 0 1 
BinaryCodec decode:abc

 

Down:

commons-codec-1.10-src.zip

commons-codec-1.10.zip

apidocs.zip

 

OVER

推荐您阅读更多有关于“ apache commons Codec ”的文章

上一篇:JDBC读写大数据字段示例 下一篇:手机端使用提示样式

猜你喜欢

发表评论: