Java String Decode decode(String value, String encoding)

Here you can find the source of decode(String value, String encoding)

Description

Decodes an encoded String.

License

Open Source License

Parameter

Parameter Description
value <code>String</code> to be translated.
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 String.

Declaration

public static String decode(String value, String encoding) throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
/**//ww w.  j a va 2 s  .c om
 * Copyright (c) 2000-2018 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.
 */

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * 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

  1. decode(String str)
  2. decode(String str)
  3. decode(String str, String encoding)
  4. decode(String string, String encoding)
  5. decode(String text)
  6. decode3(String str)
  7. decodeString(String s)
  8. decodeString(String s, String charset)
  9. decodeString(String s, String encoding)