Java URL Decode decode(String s)

Here you can find the source of decode(String s)

Description

decode

License

Open Source License

Declaration

public static String decode(String s) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w .j  a  va2 s. c om*/
 * Created on Mar 21, 2006 3:09:00 PM
 * Copyright (C) Azureus Software, Inc, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program 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 for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.io.UnsupportedEncodingException;
import java.net.*;

public class Main {
    public static String decode(String s) {
        if (s == null) {
            return "";
        }
        try {
            try {
                return (URLDecoder.decode(s, "UTF-8"));

            } catch (IllegalArgumentException e) {

                // handle truncated encodings somewhat gracefully

                int pos = s.lastIndexOf("%");

                if (pos >= s.length() - 2) {

                    return (URLDecoder.decode(s.substring(0, pos), "UTF-8"));
                }

                throw (e);
            }
        } catch (UnsupportedEncodingException e) {

            return (URLDecoder.decode(s));
        }
    }
}

Related

  1. decode(String input)
  2. decode(String path)
  3. decode(String path, String encoding)
  4. decode(String raw)
  5. decode(String s)
  6. decode(String s)
  7. decode(String s)
  8. decode(String s)
  9. decode(String s, boolean formDecode)