Java URL Encode encodeURIComponent(String input)

Here you can find the source of encodeURIComponent(String input)

Description

Function to convert a given string into URI encoded format.

License

Apache License

Parameter

Parameter Description
input the source string

Return

the encoded string

Declaration

public static String encodeURIComponent(String input) 

Method Source Code

//package com.java2s;
/**/*from   w  w  w . j  av a  2  s  .c o m*/
 * Copyright (C) 2010, Sandeep Gupta
 * http://www.sangupta.com
 * 
 * The file is licensed under the 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.UnsupportedEncodingException;

public class Main {
    /**
     * Characters that are allowed in a URI.
     */
    public static final String ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()";

    /**
     * Function to convert a given string into URI encoded format.
     * 
     * @param input the source string
     * @return the encoded string
     */
    public static String encodeURIComponent(String input) {
        if (input == null || input.trim().length() == 0) {
            return input;
        }

        int l = input.length();
        StringBuilder o = new StringBuilder(l * 3);
        try {
            for (int i = 0; i < l; i++) {
                String e = input.substring(i, i + 1);
                if (ALLOWED_CHARS.indexOf(e) == -1) {
                    byte[] b = e.getBytes("utf-8");
                    o.append(getHex(b));
                    continue;
                }
                o.append(e);
            }
            return o.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return input;
    }

    /**
     * Function to give a HEX representation of the byte array.
     * 
     * @param bytes the source byte array
     * @return the HEX coded string representing the byte array
     */
    private static String getHex(byte bytes[]) {
        StringBuilder o = new StringBuilder(bytes.length * 3);
        for (int i = 0; i < bytes.length; i++) {
            int n = (int) bytes[i] & 0xff;
            o.append("%");
            if (n < 0x10) {
                o.append("0");
            }
            o.append(Long.toString(n, 16).toUpperCase());
        }
        return o.toString();
    }
}

Related

  1. encodeArray(final String[] val)
  2. encodeForUrl(final String s)
  3. encodeSrcUrl(String fullUri)
  4. encodeStringURL(String str)
  5. encodeUri(String url)
  6. encodeURL(BitSet urlsafe, byte[] bytes)
  7. encodeUrl(final String input)
  8. encodeURL(String s)
  9. encodeURL(String s)