Java String Encode by Charset pathEncode(String path, Charset charset)

Here you can find the source of pathEncode(String path, Charset charset)

Description

Uri Encode a Path Fragment.

License

Apache License

Parameter

Parameter Description
path containing the path fragment.
charset to use.

Return

the encoded path fragment.

Declaration

public static String pathEncode(String path, Charset charset) 

Method Source Code

//package com.java2s;
/**/*from ww w .  ja va  2 s.c  o  m*/
 * Copyright 2012-2018 The Feign Authors
 *
 * 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.UnsupportedEncodingException;

import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final String PATH_RESERVED_CHARACTERS = "/=@:!$&\'(),;~";
    private static final Pattern PCT_ENCODED_PATTERN = Pattern.compile("%[0-9A-Fa-f][0-9A-Fa-f]");

    /**
     * Uri Encode a Path Fragment.
     *
     * @param path containing the path fragment.
     * @param charset to use.
     * @return the encoded path fragment.
     */
    public static String pathEncode(String path, Charset charset) {
        return encodeReserved(path, PATH_RESERVED_CHARACTERS, charset);
    }

    /**
     * Encodes the value, preserving all reserved characters.. Values that are already pct-encoded are
     * ignored.
     *
     * @param value inspect.
     * @param reserved characters to preserve.
     * @param charset to use.
     * @return a new String with the reserved characters preserved.
     */
    public static String encodeReserved(String value, String reserved, Charset charset) {
        /* value is encoded, we need to split it up and skip the parts that are already encoded */
        Matcher matcher = PCT_ENCODED_PATTERN.matcher(value);

        if (!matcher.find()) {
            return encodeChunk(value, reserved, charset);
        }

        int length = value.length();
        StringBuilder encoded = new StringBuilder(length + 8);
        int index = 0;
        do {
            /* split out the value before the encoded value */
            String before = value.substring(index, matcher.start());

            /* encode it */
            encoded.append(encodeChunk(before, reserved, charset));

            /* append the encoded value */
            encoded.append(matcher.group());

            /* update the string search index */
            index = matcher.end();
        } while (matcher.find());

        /* append the rest of the string */
        String tail = value.substring(index, length);
        encoded.append(encodeChunk(tail, reserved, charset));
        return encoded.toString();
    }

    /**
     * Encode a Uri Chunk, ensuring that all reserved characters are also encoded.
     *
     * @param value to encode.
     * @param reserved characters to evaluate.
     * @param charset to use.
     * @return an encoded uri chunk.
     */
    private static String encodeChunk(String value, String reserved, Charset charset) {
        StringBuilder encoded = null;
        int length = value.length();
        int index = 0;
        for (int i = 0; i < length; i++) {
            char character = value.charAt(i);
            if (reserved.indexOf(character) != -1) {
                if (encoded == null) {
                    encoded = new StringBuilder(length + 8);
                }

                if (i != index) {
                    /* we are in the middle of the value, so we need to encode mid string */
                    encoded.append(urlEncode(value.substring(index, i), charset));
                }
                encoded.append(character);
                index = i + 1;
            }
        }

        /* if there are no reserved characters, encode the original value */
        if (encoded == null) {
            return urlEncode(value, charset);
        }

        /* encode the rest of the string */
        if (index < length) {
            encoded.append(urlEncode(value.substring(index, length), charset));
        }
        return encoded.toString();

    }

    /**
     * Uri Encode a String using the provided charset.
     *
     * @param value to encode.
     * @param charset to use.
     * @return the encoded value.
     */
    private static String urlEncode(String value, Charset charset) {
        try {
            String encoded = URLEncoder.encode(value, charset.toString());

            /*
             * url encoding is not equivalent to URI encoding, there are few differences, namely dealing
             * with spaces, !, ', (, ), and ~ characters. we will need to manually process those values.
             */
            return encoded.replaceAll("\\+", "%20").replaceAll("\\%21", "!").replaceAll("\\%27", "'")
                    .replaceAll("\\%28", "(").replaceAll("\\%29", ")").replaceAll("\\%7E", "~")
                    .replaceAll("\\%2B", "+");

        } catch (UnsupportedEncodingException uee) {
            /* since the encoding is not supported, return the original value */
            return value;
        }
    }
}

Related

  1. getEncoder(Charset charset)
  2. getEncoder(Charset charset)
  3. getEncoder(String charset)
  4. getHeader(Charset encoding)
  5. loadAsString(String locationPattern, Charset encoding)
  6. urlEncode(final String content, final Charset charset, final BitSet safechars, final boolean blankAsPlus)
  7. urlEncode(final String str, final Charset encoding)
  8. urlEncode(String url, Charset cs)