Decodes an encoded UTF8 String. - Java java.lang

Java examples for java.lang:String UTF

Description

Decodes an encoded UTF8 String.

Demo Code

/**//from   ww w .  j  ava2 s.  c  om
 * Copyright (c) 2000-2016 Liferay, Inc. All rights reserved.
 *
 * 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.java2s;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * Decodes an encoded UTF8 String.
     *
     * @param   value  <code>String</code> to be translated.
     *
     * @return  the translated <code>String</code>.
     */
    public static String decodeUTF(String value) {
        String decodedValue = null;

        try {
            decodedValue = decode(value, "UTF8");
        } catch (UnsupportedEncodingException e) {
            // TODO - error handling
            // should never happen for this method because the
            // character encoding is constant
        }

        return decodedValue;
    }

    /**
     * Decodes an encoded String.
     *
     * @param      value     <code>String</code> to be translated.
     * @param      encoding  the Java alias for the character encoding to be used to convert byte sequences into
     *                       characters(e.g. <code>"UTF8"</code>).
     *
     * @return     the translated <code>String</code>.
     *
     * @exception  UnsupportedEncodingException  if the given encoding is not a recognised character encoding.
     */
    public static String decode(String value, String encoding)
            throws UnsupportedEncodingException {

        // optimization!
        // determine if decoding is actually required
        if (!needsDecoding(value)) {
            return value;
        }

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int charIndex = 0;
        int length = value.length();

        while (charIndex < length) {
            char aChar = value.charAt(charIndex);

            if (aChar == '%') {

                do {
                    int byteVal = (Character.digit(
                            value.charAt(charIndex + 1), 16) << 4)
                            | Character.digit(value.charAt(charIndex + 2),
                                    16);
                    bos.write(byteVal);
                    charIndex += 3;
                } while ((charIndex < length)
                        && ((aChar = value.charAt(charIndex)) == '%'));
            } else {

                if (aChar == '+') {
                    bos.write(' ');
                } else {
                    bos.write(aChar);
                }

                charIndex++;
            }
        }

        return bos.toString(encoding);
    }

    /**
     * Determine if a value needs to be decoded using HTTPUtils.decode() This method assumes that a value is encoded. As
     * such, the existence of '%' (leading character of an encoded sequence) or '+' (sometimes used to replace <space>
     * characters indicates that decoding IS required.
     */
    public static final boolean needsDecoding(String token) {

        if (token == null) {
            return false;
        }

        return (token.indexOf('+') != -1) || (token.indexOf('%') != -1);
    }
}

Related Tutorials