Java URI Decode decodeURI(CharSequence uriCharSequence, String charset)

Here you can find the source of decodeURI(CharSequence uriCharSequence, String charset)

Description

Decodes an encoded URI sequence according to the given charset This is able to decode URL-encoded characters but also leaves existing Non-ASCII characters intact (unlike Common HttpClients URIUtil)

License

Apache License

Parameter

Parameter Description
uriCharSequence The URI sequence
charset The charset used to decode

Exception

Parameter Description
UnsupportedEncodingException an exception
MalformedURLException an exception

Return

The decoded string

Declaration

public static String decodeURI(CharSequence uriCharSequence, String charset)
        throws UnsupportedEncodingException, MalformedURLException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2009, 2010 Innovation Gate GmbH
 * /*w  ww  .j  ava  2s . c o  m*/
 * 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;

import java.net.MalformedURLException;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Decodes an encoded URI sequence according to the given charset
     * This is able to decode URL-encoded characters but also leaves existing Non-ASCII characters intact (unlike Common HttpClients URIUtil)
     * @param uriCharSequence The URI sequence
     * @param charset The charset used to decode
     * @return The decoded string
     * @throws UnsupportedEncodingException
     * @throws MalformedURLException
     */
    public static String decodeURI(CharSequence uriCharSequence, String charset)
            throws UnsupportedEncodingException, MalformedURLException {

        if (uriCharSequence == null) {
            return null;
        }
        String uri = uriCharSequence.toString();

        int oi = 0; // output index
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        for (int i = 0; i < uri.length(); i++) {
            char c = uri.charAt(i);
            if (c == '%' && i + 2 <= uri.length()) {
                byte high = (byte) Character.digit((char) uri.charAt(++i), 16);
                byte low = (byte) Character.digit((char) uri.charAt(++i), 16);
                if (high == -1 || low == -1) {
                    throw new MalformedURLException("Invalid escape pattern");

                }
                byte aByte = (byte) ((high << 4) + low);
                out.write(aByte);
            } else if (c == '+') {
                out.write(' ');
            } else {
                byte[] bytes = String.valueOf(c).getBytes(charset);
                out.write(bytes, 0, bytes.length);
            }
        }

        return out.toString(charset);
    }

    /**
     * Creates a new list that contains the string representations
     * of all elements of the original list
     * null values are preserved.
     * 
     * @param listOriginal
     * @return The list with all elements converted to their string representation
     */
    public static List<String> toString(List<Object> listOriginal) {

        List<String> list = new ArrayList<String>();
        Object elem;
        for (int i = 0; i < listOriginal.size(); i++) {
            elem = listOriginal.get(i);
            if (elem != null) {
                list.add(String.valueOf(elem));
            } else {
                list.add(null);
            }
        }

        return list;

    }
}

Related

  1. decode(String encodedURI)
  2. decode(String uri)
  3. decodeToUtf8(String uri)
  4. decodeUnreserved(URI address)
  5. decodeURI(java.net.URI uri)
  6. decodeURI(String s)
  7. decodeURI(String str, boolean fullUri)
  8. decodeUri(String string)