Java tutorial
/** * Copyright (c) 2005-2009 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * */ package com.lakeside.core.utils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import com.google.common.io.BaseEncoding; /** * ?????. * * ?Commons-Codec,Commons-Lang?JDK???. * */ public class EncodeUtils { private static final String DEFAULT_URL_ENCODING = "UTF-8"; /** * Used to build output as Hex */ private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /** * Used to build output as Hex */ private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; /** * Hex?. */ public static String hexEncode(byte[] data) { return hexEncode(data, true); } /** * Hex?. */ public static String hexEncode(byte[] data, boolean toLowerCase) { char[] toDigits = toLowerCase ? DIGITS_LOWER : DIGITS_UPPER; int l = data.length; char[] out = new char[l << 1]; // two characters form the hex value. for (int i = 0, j = 0; i < l; i++) { out[j++] = toDigits[(0xF0 & data[i]) >>> 4]; out[j++] = toDigits[0x0F & data[i]]; } return new String(out); } /** * Hex?. */ public static byte[] hexDecode(String input) { char[] data = input.toCharArray(); int len = data.length; if ((len & 0x01) != 0) { throw new RuntimeException("Odd number of characters."); } byte[] out = new byte[len >> 1]; // two characters form the hex value. for (int i = 0, j = 0; j < len; i++) { int f = toDigit(data[j], j) << 4; j++; f = f | toDigit(data[j], j); j++; out[i] = (byte) (f & 0xFF); } return out; } protected static int toDigit(char ch, int index) { int digit = Character.digit(ch, 16); if (digit == -1) { throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index); } return digit; } /** * Base64?. */ public static String base64Encode(byte[] input) { return new String(BaseEncoding.base64().encode(input)); } /** * Base64?, URL(Base64URL?+,/=, ?RFC3548). */ public static String base64UrlSafeEncode(byte[] input) { return BaseEncoding.base64Url().encode(input); } /** * Base64?. */ public static byte[] base64Decode(String input) { return BaseEncoding.base64().decode(input); } /** * URL ?, EncodeUTF-8. */ public static String urlEncode(String input) { return urlEncode(input, DEFAULT_URL_ENCODING); } /** * URL ?. */ public static String urlEncode(String input, String encoding) { try { return URLEncoder.encode(input, encoding); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException("Unsupported Encoding Exception", e); } } /** * URL ?, EncodeUTF-8. */ public static String urlDecode(String input) { return urlDecode(input, DEFAULT_URL_ENCODING); } /** * URL ?. */ public static String urlDecode(String input, String encoding) { try { return URLDecoder.decode(input, encoding); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException("Unsupported Encoding Exception", e); } } }