Java URI Encode encodeIfNeeded(String uri)

Here you can find the source of encodeIfNeeded(String uri)

Description

encode If Needed

License

Open Source License

Declaration

public static String encodeIfNeeded(String uri)
            throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;
import java.net.URLEncoder;

public class Main {
    public static String encodeIfNeeded(String uri)
            throws UnsupportedEncodingException {
        String decodedUri = URLDecoder.decode(uri, "UTF-8");
        // It is necessary to encode it
        if (uri.compareTo(decodedUri) == 0)
            return URLEncoder.encode(uri, "UTF-8");
        // If was already encoded
        else/*  ww w.j a va2 s .  co  m*/
            return uri;
    }
}

Related

  1. encode(String str, boolean fullUri)
  2. encode(String uri)
  3. encodeForURI(String uriPart)
  4. encodeUri(final String uri)
  5. encodeUri(final URI uri)
  6. encodeURI(String s)
  7. encodeUri(String string)