Import java.io.UnsupportedEncodingException;
Import Java.net.URLDecoder;
Import Java.net.URLEncoder;
Import org.apache.commons.codec.DecoderException;
Import org.apache.commons.codec.binary.Base64;
Import Org.apache.commons.codec.binary.Hex;
Import Org.apache.commons.lang.StringEscapeUtils;
/**
* Coding in various formats of the tool class.
*
* Integrated Commons-codec,commons-lang and JDK provided by the codec method.
*
*
*/
public class Encodeutils {
private static final String default_url_encoding = "UTF-8";
/**
* Hex code.
*/
/*public static String Hexencode (byte[] input) {
return hex.encodehexstring (input);
}*/
/**
* Hex decoding.
*/
public static byte[] Hexdecode (String input) {
try {
Return Hex.decodehex (Input.tochararray ());
catch (Decoderexception e) {
throw new IllegalStateException ("Hex Decoder exception", e);
}
}
/**
* Base64 code.
*/
public static String Base64Encode (byte[] input) {
return new String (base64.encodebase64 (input));
}
/**
* Base64 encoding, URL security (will Base64 the URL in the illegal characters,/= to other characters, see RFC3548).
*/
public static String Base64urlsafeencode (byte[] input) {
return base64.encodebase64urlsafestring (input);
}
/**
* Base64 decoding.
*/
public static byte[] Base64decode (String input) {
return base64.decodebase64 (input);
}
/**
* URL encoding, encode defaults to UTF-8.
*/
public static string UrlEncode (string input) {
try {
return Urlencoder.encode (input, default_url_encoding);
catch (Unsupportedencodingexception e) {
throw new IllegalArgumentException ("Unsupported Encoding Exception", e);
}
}
/**
* URL decoding, encode defaults to UTF-8.
*/
public static string UrlDecode (string input) {
try {
return Urldecoder.decode (input, default_url_encoding);
catch (Unsupportedencodingexception e) {
throw new IllegalArgumentException ("Unsupported Encoding Exception", e);
}
}
/**
* Html transcoding.
*/
public static string Htmlescape (string html) {
return stringescapeutils.escapehtml (HTML);
}
/**
* Html decoding.
*/
public static string Htmlunescape (String htmlescaped) {
Return stringescapeutils.unescapehtml (htmlescaped);
}
/**
* Xml transcoding.
*/
public static string Xmlescape (string xml) {
return Stringescapeutils.escapexml (XML);
}
/**
* XML decoding.
*/
public static string Xmlunescape (String xmlescaped) {
Return Stringescapeutils.unescapexml (xmlescaped);
}
}