com.oakhole.utils.EncodeUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.oakhole.utils.EncodeUtils.java

Source

/*
 * Copyright (c) 2013-2014. Powered by http://oakhole.com .
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.oakhole.utils;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * encode utils.
 * 
 * @author Lingo
 */
public class EncodeUtils {
    /** default url encoding. */
    private static final String DEFAULT_URL_ENCODING = "UTF-8";

    /** unprintable char code. */
    private static final int UNPRINTABLE_CHAR_CODE = 16;

    /** ansi char code. */
    private static final int ANSI_CHAR_CODE = 256;

    /** hex. */
    private static final int HEX = 16;

    /** unicode length. */
    private static final int UNICODE_LENGTH = "\\u0000".length();

    /** ansi length. */
    private static final int ANSI_LENGTH = "%FF".length();

    /** protected constructor. */
    protected EncodeUtils() {
    }

    /**
     * Hex?.
     * 
     * @param input
     *            byte[]
     * @return String
     */
    public static String hexEncode(byte[] input) {
        return Hex.encodeHexString(input);
    }

    /**
     * Hex?.
     * 
     * @param input
     *            String
     * @return byte[]
     */
    public static byte[] hexDecode(String input) {
        try {
            return Hex.decodeHex(input.toCharArray());
        } catch (DecoderException e) {
            throw new IllegalStateException("Hex Decoder exception", e);
        }
    }

    /**
     * Base64?.
     * 
     * @param input
     *            byte[]
     * @return String
     */
    public static String base64Encode(byte[] input) throws UnsupportedEncodingException {
        return new String(Base64.encodeBase64(input), "UTF-8");
    }

    /**
     * Base64?, URL(Base64URL?+,/=, ?RFC3548).
     * 
     * @param input
     *            byte[]
     * @return String
     */
    public static String base64UrlSafeEncode(byte[] input) {
        return Base64.encodeBase64URLSafeString(input);
    }

    /**
     * Base64?.
     * 
     * @param input
     *            String
     * @return byte[]
     */
    public static byte[] base64Decode(String input) {
        return Base64.decodeBase64(input);
    }

    /**
     * URL ?, EncodeUTF-8.
     * 
     * @param input
     *            String
     * @return String
     */
    public static String urlEncode(String input) throws UnsupportedEncodingException {
        return URLEncoder.encode(input, DEFAULT_URL_ENCODING);
    }

    /**
     * URL ?, EncodeUTF-8.
     * 
     * @param input
     *            String
     * @return String
     */
    public static String urlDecode(String input) throws UnsupportedEncodingException {
        return URLDecoder.decode(input, DEFAULT_URL_ENCODING);
    }

    /**
     * Html ?.
     * 
     * @param html
     *            String
     * @return String
     */
    public static String htmlEscape(String html) {
        return StringUtils.escapeHtml(html);
    }

    /**
     * Html ?.
     * 
     * @param htmlEscaped
     *            String
     * @return String
     */
    public static String htmlUnescape(String htmlEscaped) {
        return StringUtils.unescapeHtml(htmlEscaped);
    }

    /**
     * Xml ?.
     * 
     * @param xml
     *            String
     * @return String
     */
    public static String xmlEscape(String xml) {
        return StringUtils.escapeXml(xml);
    }

    /**
     * Xml ?.
     * 
     * @param xmlEscaped
     *            String
     * @return String
     */
    public static String xmlUnescape(String xmlEscaped) {
        return StringUtils.unescapeXml(xmlEscaped);
    }

    /**
     * jsescape.
     * 
     * @param src
     *            String
     * @return String
     */
    public static String escapeJS(String src) {
        int i;
        char j;
        StringBuffer tmp = new StringBuffer();
        tmp.ensureCapacity(src.length() * UNICODE_LENGTH);

        for (i = 0; i < src.length(); i++) {
            j = src.charAt(i);

            if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j)) {
                tmp.append(j);
            } else if (j < ANSI_CHAR_CODE) {
                tmp.append("%");

                if (j < UNPRINTABLE_CHAR_CODE) {
                    tmp.append("0");
                }

                tmp.append(Integer.toString(j, HEX));
            } else {
                tmp.append("%u");
                tmp.append(Integer.toString(j, HEX));
            }
        }

        return tmp.toString();
    }

    /**
     * jsunescape.
     * 
     * @param src
     *            String
     * @return String
     */
    public static String unescapeJS(String src) {
        StringBuffer tmp = new StringBuffer();
        tmp.ensureCapacity(src.length());

        int lastPos = 0;
        int pos = 0;
        char ch;

        while (lastPos < src.length()) {
            pos = src.indexOf('%', lastPos);

            if (pos == lastPos) {
                if (src.charAt(pos + 1) == 'u') {
                    ch = (char) Integer.parseInt(src.substring(pos + 2, pos + UNICODE_LENGTH), HEX);
                    tmp.append(ch);
                    lastPos = pos + UNICODE_LENGTH;
                } else {
                    ch = (char) Integer.parseInt(src.substring(pos + 1, pos + ANSI_LENGTH), HEX);
                    tmp.append(ch);
                    lastPos = pos + ANSI_LENGTH;
                }
            } else {
                if (pos == -1) {
                    tmp.append(src.substring(lastPos));
                    lastPos = src.length();
                } else {
                    tmp.append(src.substring(lastPos, pos));
                    lastPos = pos;
                }
            }
        }

        return tmp.toString();
    }
}