Java String Encode encodeRfc5849(final String value)

Here you can find the source of encodeRfc5849(final String value)

Description

Percent encodeRfc5849 the value as specified by the RFC5849 (3.6).

License

Apache License

Declaration

static String encodeRfc5849(final String value) 

Method Source Code

//package com.java2s;
/* Utils.java// ww  w.  j  av  a 2 s. c  om
 *
 * Created: 2012-10-01 (Year-Month-Day)
 * Character encoding: UTF-8
 *
 ****************************************** LICENSE *******************************************
 *
 * Copyright (c) 2012 - 2013 XIAM Solutions B.V. (http://www.xiam.nl)
 *
 * 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;

public class Main {
    /**
     * The canonical character set name for {@code UTF-8}.
     * {@code UTF-8} is the only character set used by JSON.
     */
    static final String UTF8 = "UTF-8";

    /**
     * Percent encodeRfc5849 the value as specified by the RFC5849 (3.6).
     */
    static String encodeRfc5849(final String value) {
        try {
            return URLEncoder.encode(value, UTF8)
                    // now correct difference between URLEncoder and RFC5849 ...
                    // not efficient, but working :)
                    .replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
        } catch (UnsupportedEncodingException ex) {
            throw new AssertionError(ex); // UTF-8 is always supported
        }
    }
}

Related

  1. encodeParams(Map nameValuePairs)
  2. encodeParams(String encoding, String... params)
  3. EncodePath(String path)
  4. encodePath(String path)
  5. encodePath(String str)
  6. encodeSpecialMdxCharactersInNames(String name)
  7. encodeState(Map params)
  8. encodeStr(String str)
  9. encodeStr(String str)