Java String Decode decode(String s, String enc, boolean plusToSpace)

Here you can find the source of decode(String s, String enc, boolean plusToSpace)

Description

Decodes a String URL encoded URL

License

Open Source License

Parameter

Parameter Description
s the string
enc the encoding (defaults to UTF-8 if null)
plusToSpace true if plus signs be converted to spaces

Return

a decoded string

Declaration

public static String decode(String s, String enc, boolean plusToSpace) 

Method Source Code

//package com.java2s;
/*/* w w  w .  j ava  2 s . c  o m*/
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores
 * CA 94065 USA or visit www.oracle.com if you need additional information or
 * have any questions.
 */

import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * Decodes a String URL encoded URL
     * 
     * @param s the string
     * @param enc the encoding (defaults to UTF-8 if null)
     * @param plusToSpace true if plus signs be converted to spaces
     * @return a decoded string
     */
    public static String decode(String s, String enc, boolean plusToSpace) {
        boolean modified = false;
        if (enc == null || enc.length() == 0) {
            enc = "UTF-8";
        }
        int numChars = s.length();
        StringBuilder sb = new StringBuilder(numChars > 500 ? numChars / 2 : numChars);
        int i = 0;

        char c;
        byte[] bytes = null;
        while (i < numChars) {
            c = s.charAt(i);
            switch (c) {
            case '+':
                if (plusToSpace) {
                    sb.append(' ');
                } else {
                    sb.append('+');
                }
                i++;
                modified = true;
                break;

            case '%':
                try {
                    if (bytes == null) {
                        bytes = new byte[(numChars - i) / 3];
                    }

                    int pos = 0;

                    while (((i + 2) < numChars) && (c == '%')) {
                        bytes[pos++] = (byte) Integer.parseInt(s.substring(i + 1, i + 3), 16);
                        i += 3;
                        if (i < numChars) {
                            c = s.charAt(i);
                        }
                    }

                    if ((i < numChars) && (c == '%')) {
                        throw new IllegalArgumentException("Illegal URL % character: " + s);
                    }

                    try {
                        sb.append(new String(bytes, 0, pos, enc));
                    } catch (UnsupportedEncodingException e) {
                        throw new RuntimeException(e.toString());
                    }
                } catch (NumberFormatException e) {
                    throw new IllegalArgumentException("Illegal URL encoding: " + s);
                }
                modified = true;
                break;

            default:
                sb.append(c);
                i++;
                break;
            }
        }

        if (modified) {
            return sb.toString();
        }
        return s;
    }
}

Related

  1. decode(String s)
  2. decode(String s)
  3. decode(String s)
  4. decode(String s, Class clazz)
  5. decode(String s, String charset1, String charset2)
  6. decode(String s, String encoding)
  7. decode(String str)
  8. decode(String str)
  9. decode(String str, String encoding)