Java String Encode encodeConvenience(String str)

Here you can find the source of encodeConvenience(String str)

Description

Performs safe URI-escaping of the '%' and ' ' characters in the string for convenience sake.

License

Apache License

Parameter

Parameter Description
str String to encode.

Return

the string with stray percents and spaces percent-encoded.

Declaration

public static String encodeConvenience(String str) 

Method Source Code

//package com.java2s;
/*/*from  w  ww .  j  a  v a2s . co m*/
 * Copyright 2014 Google. Inc.
 *
 * 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.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern CONVENIENCE_ENCODABLE = Pattern.compile("\\%(?![0-9a-fA-F][0-9a-fA-F])|\\ ");
    private static final int ENCODE_ALLOWANCE = 16;
    private static final char[] HEX_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
            'E', 'F' };

    /**
     * Performs safe URI-escaping of the '%' and ' ' characters in the string
     * for convenience sake. This is NOT a complete percent-encoding procedure.
     * The idea is to escape these characters (and only these characters) across
     * the entire URI string so that addresses containing them can remain in a
     * readable form in user code.
     *  
     * <p>The Java-included {@link java.net.URLEncoder} class has similar
     * functionality, but its encoding is designed specifically for the query
     * portion of a URL, and does not work universally across an entire URI.
     * For example, it will encode {@code "/my keys/bank"} as
     * {@code "%2Fmy+keys%2Fbank"}, whereas this method will encode it as
     * {@code "/my%20keys/bank"}. The escaped backslashes and conversion of
     * spaces to {@code '+'} will effectively change the meaning of the address,
     * and this is undesirable behavior. 
     * 
     * @param str String to encode.
     * 
     * @return the string with stray percents and spaces percent-encoded.
     */
    public static String encodeConvenience(String str) {
        Matcher m = CONVENIENCE_ENCODABLE.matcher(str);
        boolean found = m.find();
        if (found) {
            final char[] hexTable = HEX_TABLE;
            StringBuilder sb = new StringBuilder(str.length() + ENCODE_ALLOWANCE);
            int findStart = 0;
            do {
                final int pos = m.start();
                sb.append(str, findStart, pos);

                // Percent-encode the encodable character
                final char c = str.charAt(pos);
                sb.append('%').append(hexTable[c >>> 4]).append(hexTable[c & 7]);

                findStart = m.end();
                found = m.find();
            } while (found);

            sb.append(str, findStart, str.length());
            return sb.toString();
        }
        return str;
    }
}

Related

  1. encode(String text, String encoding)
  2. encode(String text, String encoding)
  3. encode(String val, String encoding)
  4. encode(String value, String encoding)
  5. encodeBase64(String str)
  6. encodeDataPair(final StringBuilder buffer, final String key, final String value)
  7. encodeDoublePercent(String input)
  8. encodeDownloadFileName(String s)
  9. encodeFilename(final String filename, final String userAgent)