Utility class for URL decoding. : Utilities « Network Protocol « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Network Protocol » UtilitiesScreenshots 
Utility class for URL decoding.
      
/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2002 - 2007 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
//package flex.messaging.util;

import java.io.UnsupportedEncodingException;


/**
 * Utility class for URL decoding.
 
 *@exclude
 */
public final class URLDecoder
{
    public static String decode(String s)
    {
        try
        {
            return decode(s, "UTF8");
        }
        catch (UnsupportedEncodingException ex)
        {
            throw new IllegalArgumentException("UTF8");
        }
    }

    public static String decode(String s, String encthrows UnsupportedEncodingException
    {
        if (!needsDecoding(s))
        {
            return s;
        }

        int length = s.length();
        byte[] bytes = new byte[length];
        // FIXME: This needs to specify a character encoding (ASCII?)
        s.getBytes(0, length, bytes, 0);
        int k = 0;
        length = bytes.length;
        for (int i = 0; i < length; i++)
        {
            if (bytes[i== '%')
            {
                while (bytes[i + 1== '%')
                {
                    i++;
                }
                if (i < length - 2)
                {
                    bytes[k= x2c(bytes, i);
                    i += 2;
                }
                else
                {
                    throw new IllegalArgumentException(s);
                }
            }
            else if (bytes[i== '+')
            {
                bytes[k(byte)' ';
            }
            else
            {
                bytes[k= bytes[i];
            }
            k++;
        }

        return new String(bytes, 0, k, enc);
    }

    private static boolean needsDecoding(String s)
    {
        if (s == null)
        {
            return false;
        }

        int length = s.length();

        for (int i = 0; i < length; i++)
        {
            int c = (int)s.charAt(i);
            if (c == '+' || c == '%')
            {
                return true;
            }
        }

        return false;

    }

    private static byte x2c(byte[] b, int i)
    {
        int result;
        byte b1 = b[i + 1];
        byte b2 = b[i + 2];

        // return Byte.parseByte("" + (char) b1 + (char) b2, 16);

        if (b1 < '0' || (b1 > 'F' && b1 < 'a'|| b1 > 'f' ||
                b2 < '0' || (b2 > 'F' && b2 < 'a'|| b2 > 'f')
        {
            throw new IllegalArgumentException("%" (char)b1 + (char)b2);
        }

        result = b1 >= 'A' (b1 & 0xdf'A' 10 : b1 - '0';
        result *= 16;
        result += b2 >= 'A' (b2 & 0xdf'A' 10 : b2 - '0';
        return (byte)result;
    }
}

   
    
    
    
    
    
  
Related examples in the same category
1.A class that encodes URL parameter values for MIDP devices.
2.Get the listing of everyone logged on
3.Scan your computer for ports in useScan your computer for ports in use
4.Using the URL Class (GetURL.java)
5.TCP socket monitor
6.Create Socket helper
7.Implements a TCP/IP bounce utility (proxy)
8.URL utilities class that makes it easy to create new URLs based off of old URLs without having to assemble or parse them yourself
9.Download from URL
10.URL ParserURL Parser
11.A simple class to load an URL in a browser
12.Using the java API URL class, extract the http/https hostname.
13.Utility class for URL encoding.
14.Allows easy downloading of files from HTTP sites
15.enum Http Status
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.