Here you can find the source of encodeStringURL(String str)
Encodes a given string.
Parameter | Description |
---|---|
str | String. |
public static String encodeStringURL(String str)
//package com.java2s; /* Util.java//from w w w.j a v a 2s . co m * * Networking ME * Copyright (c) 2013 eMob Tech (http://www.emobtech.com/) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; public class Main { /** * <p> * UTF-8 encoding. * </p> */ public static final String UTF8 = "UTF-8"; /** * <p> * Encodes a given string. * </p> * @param str String. * @return Encoded string. */ public static String encodeStringURL(String str) { if (str.indexOf('%') == -1) { return encodeStringURL(str, UTF8); } else { return str; } } /** * <p> * Encodes a given string with a encoding. * </p> * @param str String. * @param encoding Encoding. * @return Enconded string. */ public static String encodeStringURL(String str, String encoding) { if (isEmptyString(str)) { return str; } // if (isEmptyString(encoding)) { encoding = getSystemProperty("microedition.encoding", UTF8); } // ByteArrayInputStream bIn; // try { bIn = new ByteArrayInputStream(str.getBytes(encoding)); } catch (UnsupportedEncodingException e) { bIn = new ByteArrayInputStream(str.getBytes()); } // int c = bIn.read(); StringBuffer ret = new StringBuffer(); // while (c >= 0) { if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '*' || c == '_') { ret.append((char) c); } else if (c == ' ') { ret.append("%20"); } else { if (c < 128) { ret.append(getHexChar(c)); } else if (c < 224) { ret.append(getHexChar(c)); ret.append(getHexChar(bIn.read())); } else if (c < 240) { ret.append(getHexChar(c)); ret.append(getHexChar(bIn.read())); ret.append(getHexChar(bIn.read())); } } // c = bIn.read(); } // return ret.toString(); } /** * <p> * Checks whether the given string is null or empty. * </p> * @param str The string. * @return true null/empty. */ public static boolean isEmptyString(String str) { return str == null || str.trim().length() == 0; } /** * <p> * Returns the system property value or a default value if it is * <code>null</code>. * </p> * @param key Property key. * @param defaultValue Default value. * @return Value. */ public static String getSystemProperty(String key, String defaultValue) { String value = System.getProperty(key); // return isEmptyString(value) ? defaultValue : value; } /** * <p> * Returns the hex value of a char. * </p> * @param c Char. * @return Hex value. */ public static String getHexChar(int c) { return (c < 16 ? "%0" : "%") + Integer.toHexString(c).toUpperCase(); } /** * <p> * Returns the string of a given bytes. * </p> * @param bytes Bytes. * @return String. */ public static String toString(byte[] bytes) { if (bytes == null || bytes.length == 0) { return ""; } // try { return new String(bytes, UTF8); } catch (UnsupportedEncodingException e) { return new String(bytes); } } }