2009年6月9日

Apache Commons Codec

Apache Commons Codec

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.BinaryCodec;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.net.BCodec;
import org.apache.commons.codec.net.QCodec;
import org.apache.commons.codec.net.QuotedPrintableCodec;
import org.apache.commons.codec.net.URLCodec;

public class Main {

public static void main(String[] args) throws Exception {
String str = "Hello World";
byte[] data = str.getBytes();

System.out.println(DigestUtils.md5Hex(str));
System.out.println(DigestUtils.shaHex(data));

System.out.println(str);

byte[] base64Encoded = Base64.encodeBase64(data);
System.out.println(new String(base64Encoded));

char[] hexEncoded = Hex.encodeHex(base64Encoded);
System.out.println(hexEncoded);
byte[] hexDecoded = Hex.decodeHex(hexEncoded);
System.out.println(new String(hexDecoded));


byte[] base64Decoded = Base64.decodeBase64(base64Encoded);
System.out.println(new String(base64Decoded));

BinaryCodec bc = new BinaryCodec();

byte[] binEncoded = bc.encode(base64Encoded);
System.out.println(new String(binEncoded));
byte[] binDecoded = bc.decode(binEncoded);
System.out.println(new String(binDecoded));


BCodec bCodec = new BCodec();

String bEncode = bCodec.encode(str);
System.out.println(bEncode);
System.out.println(bCodec.decode(bEncode));

QCodec qCodec = new QCodec();

String qEncode = qCodec.encode(str);
System.out.println(qEncode);
System.out.println(qCodec.decode(qEncode));

URLCodec urlCodec = new URLCodec();

str = "<&?%;>";
System.out.println(str);

String urlEncoded = urlCodec.encode(str);
System.out.println(urlEncoded);

String urlDecoded = urlCodec.decode(urlEncoded);
System.out.println(urlDecoded);

QuotedPrintableCodec quotedPrintableCodec = new QuotedPrintableCodec();

str = "[\b\r\n\0]";
System.out.println(str);

String cpEncoded = quotedPrintableCodec.encode(str);
System.out.println(cpEncoded);

String cpDecoded = quotedPrintableCodec.decode(cpEncoded);
System.out.println(cpDecoded);
}

}
/* -- output --
b10a8db164e0754105b7a99be72e3fe5
0a4d55a8d778e5022fab701977c5d840bbc486d0
Hello World
SGVsbG8gV29ybGQ=
5347567362473867563239796247513d
SGVsbG8gV29ybGQ=
Hello World
00111101010100010100011101100010011110010011100100110010010101100110011100111000010001110110001001110011010101100100011101010011
SGVsbG8gV29ybGQ=
=?UTF-8?B?SGVsbG8gV29ybGQ=?=
Hello World
=?UTF-8?Q?Hello World?=
Hello World
<&?%;>
%3C%26%3F%25%3B%3E
<&?%;>
[
]
[=08=0D=0A=00]
[
]
*/

沒有留言:

網誌存檔