Java URL Decode unescape(String string)

Here you can find the source of unescape(String string)

Description

Does a URL decoding of the string.

License

Apache License

Parameter

Parameter Description
string the string to decode

Exception

Parameter Description
NullPointerException if <code>string</code> is <code>null</code>.
ArrayIndexOutOfBoundsException if not enough character follow anescape character
IllegalArgumentException if the 2 characters following the escapecharacter do not represent a hex-number.

Return

the decoded string

Declaration

public static String unescape(String string) 

Method Source Code


//package com.java2s;
/*//from   w  w w  .  j a  va  2  s .co  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 {
    /**
     * Does a URL decoding of the <code>string</code>. Please note that in
     * opposite to the {@link java.net.URLDecoder} it does not transform the +
     * into spaces.
     *
     * @param string the string to decode
     * @return the decoded string
     * @throws NullPointerException           if <code>string</code> is <code>null</code>.
     * @throws ArrayIndexOutOfBoundsException if not enough character follow an
     *                                        escape character
     * @throws IllegalArgumentException       if the 2 characters following the escape
     *                                        character do not represent a hex-number.
     */
    public static String unescape(String string) {
        return unescape(string, '%');
    }

    /**
     * Does a URL decoding of the <code>string</code> using the
     * <code>escape</code> character. Please note that in opposite to the
     * {@link java.net.URLDecoder} it does not transform the + into spaces.
     *
     * @param string the string to decode
     * @param escape the escape character
     * @return the decoded string
     * @throws NullPointerException           if <code>string</code> is <code>null</code>.
     * @throws IllegalArgumentException       if the 2 characters following the escape
     *                                        character do not represent a hex-number
     *                                        or if not enough characters follow an
     *                                        escape character
     */
    private static String unescape(String string, char escape) {
        try {
            byte[] utf8 = string.getBytes("utf-8");

            // Check whether escape occurs at invalid position
            if ((utf8.length >= 1 && utf8[utf8.length - 1] == escape)
                    || (utf8.length >= 2 && utf8[utf8.length - 2] == escape)) {
                throw new IllegalArgumentException("Premature end of escape sequence at end of input");
            }

            ByteArrayOutputStream out = new ByteArrayOutputStream(utf8.length);
            for (int k = 0; k < utf8.length; k++) {
                byte b = utf8[k];
                if (b == escape) {
                    out.write((decodeDigit(utf8[++k]) << 4) + decodeDigit(utf8[++k]));
                } else {
                    out.write(b);
                }
            }

            return new String(out.toByteArray(), "utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new InternalError(e.toString());
        }
    }

    private static byte decodeDigit(byte b) {
        if (b >= 0x30 && b <= 0x39) {
            return (byte) (b - 0x30);
        } else if (b >= 0x41 && b <= 0x46) {
            return (byte) (b - 0x37);
        } else if (b >= 0x61 && b <= 0x66) {
            return (byte) (b - 0x57);
        } else {
            throw new IllegalArgumentException("Escape sequence is not hexadecimal: " + (char) b);
        }
    }
}

Related

  1. pathDecode(String path)
  2. percentDecode(String encodedString, String messageEncoding)
  3. percentDecode(String str, boolean plusToSpace)
  4. safeDecode(String s)
  5. unescape(String s)
  6. unescape(String theString)
  7. urlDecode(String s)
  8. urlDecode(String scope)
  9. URLDecode(String str)