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

Apache Commons Codec

89380 开源 | 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.



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.





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;
public class CodecTest {
 public static void main(String[] args) throws Exception{
  String str = "abc";  
  // MD5
  // SHA1
  // 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("崔素强", "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 + " ");
  b = BinaryCodec.fromAscii(c);
  System.out.println("BinaryCodec decode:" + new String(b));
  // StringUtils


Console Out:

Base64 Encode:YWJj
Base64 Decode:abc
Base32 Encode:MFRGG===
Base32 Decode:abc
URLCodec decode:崔素强
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





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

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