📄 uriutil.java
字号:
* The path may consist of a sequence of path segments separated by a * single slash "/" character. Within a path segment, the characters * "/", ";", "=", and "?" are reserved. * * @param unescaped an unescaped string * @param charset the charset * @return the escaped string * * @throws URIException if the charset is not supported * * @see #encode */ public static String encodeWithinPath(String unescaped, String charset) throws URIException { return encode(unescaped, URI.allowed_within_path, charset); } /** * Escape and encode a string regarded as the path component of an URI with * the default protocol charset. * * @param unescaped an unescaped string * @return the escaped string * * @throws URIException if the default protocol charset is not supported * * @see URI#getDefaultProtocolCharset * @see #encode */ public static String encodePath(String unescaped) throws URIException { return encodePath(unescaped, URI.getDefaultProtocolCharset()); } /** * Escape and encode a string regarded as the path component of an URI with * a given charset. * * @param unescaped an unescaped string * @param charset the charset * @return the escaped string * * @throws URIException if the charset is not supported * * @see #encode */ public static String encodePath(String unescaped, String charset) throws URIException { return encode(unescaped, URI.allowed_abs_path, charset); } /** * Escape and encode a string regarded as within the query component of an * URI with the default protocol charset. * When a query comprise the name and value pairs, it is used in order * to encode each name and value string. The reserved special characters * within a query component are being included in encoding the query. * * @param unescaped an unescaped string * @return the escaped string * * @throws URIException if the default protocol charset is not supported * * @see URI#getDefaultProtocolCharset * @see #encode */ public static String encodeWithinQuery(String unescaped) throws URIException { return encodeWithinQuery(unescaped, URI.getDefaultProtocolCharset()); } /** * Escape and encode a string regarded as within the query component of an * URI with a given charset. * When a query comprise the name and value pairs, it is used in order * to encode each name and value string. The reserved special characters * within a query component are being included in encoding the query. * * @param unescaped an unescaped string * @param charset the charset * @return the escaped string * * @throws URIException if the charset is not supported * * @see #encode */ public static String encodeWithinQuery(String unescaped, String charset) throws URIException { return encode(unescaped, URI.allowed_within_query, charset); } /** * Escape and encode a string regarded as the query component of an URI with * the default protocol charset. * When a query string is not misunderstood the reserved special characters * ("&", "=", "+", ",", and "$") within a query component, this method * is recommended to use in encoding the whole query. * * @param unescaped an unescaped string * @return the escaped string * * @throws URIException if the default protocol charset is not supported * * @see URI#getDefaultProtocolCharset * @see #encode */ public static String encodeQuery(String unescaped) throws URIException { return encodeQuery(unescaped, URI.getDefaultProtocolCharset()); } /** * Escape and encode a string regarded as the query component of an URI with * a given charset. * When a query string is not misunderstood the reserved special characters * ("&", "=", "+", ",", and "$") within a query component, this method * is recommended to use in encoding the whole query. * * @param unescaped an unescaped string * @param charset the charset * @return the escaped string * * @throws URIException if the charset is not supported * * @see #encode */ public static String encodeQuery(String unescaped, String charset) throws URIException { return encode(unescaped, URI.allowed_query, charset); } /** * Escape and encode a given string with allowed characters not to be * escaped and the default protocol charset. * * @param unescaped a string * @param allowed allowed characters not to be escaped * @return the escaped string * * @throws URIException if the default protocol charset is not supported * * @see URI#getDefaultProtocolCharset */ public static String encode(String unescaped, BitSet allowed) throws URIException { return encode(unescaped, allowed, URI.getDefaultProtocolCharset()); } /** * Escape and encode a given string with allowed characters not to be * escaped and a given charset. * * @param unescaped a string * @param allowed allowed characters not to be escaped * @param charset the charset * @return the escaped string */ public static String encode(String unescaped, BitSet allowed, String charset) throws URIException { byte[] rawdata = URLCodec.encodeUrl(allowed, EncodingUtil.getBytes(unescaped, charset)); return EncodingUtil.getAsciiString(rawdata); } /** * Unescape and decode a given string regarded as an escaped string with the * default protocol charset. * * @param escaped a string * @return the unescaped string * * @throws URIException if the string cannot be decoded (invalid) * * @see URI#getDefaultProtocolCharset */ public static String decode(String escaped) throws URIException { try { byte[] rawdata = URLCodec.decodeUrl(EncodingUtil.getAsciiBytes(escaped)); return EncodingUtil.getString(rawdata, URI.getDefaultProtocolCharset()); } catch (DecoderException e) { throw new URIException(e.getMessage()); } } /** * Unescape and decode a given string regarded as an escaped string. * * @param escaped a string * @param charset the charset * @return the unescaped string * * @throws URIException if the charset is not supported * * @see Coder#decode */ public static String decode(String escaped, String charset) throws URIException { return Coder.decode(escaped.toCharArray(), charset); } // ---------------------------------------------------------- Inner classes /** * The basic and internal utility for URI escape and character encoding and * decoding. * * @deprecated use org.apache.commons.codec.net.URLCodec */ protected static class Coder extends URI { /** * Escape and encode a given string with allowed characters not to be * escaped. * * @param unescapedComponent an unescaped component * @param allowed allowed characters not to be escaped * @param charset the charset to encode * @return the escaped and encoded string * * @throws URIException if the charset is not supported * * @deprecated use org.apache.commons.codec.net.URLCodec */ public static char[] encode(String unescapedComponent, BitSet allowed, String charset) throws URIException { return URI.encode(unescapedComponent, allowed, charset); } /** * Unescape and decode a given string. * * @param escapedComponent an being-unescaped component * @param charset the charset to decode * @return the escaped and encoded string * * @throws URIException if the charset is not supported * * @deprecated use org.apache.commons.codec.net.URLCodec */ public static String decode(char[] escapedComponent, String charset) throws URIException { return URI.decode(escapedComponent, charset); } /** * Verify whether a given string is escaped or not * * @param original given characters * @return true if the given character array is 7 bit ASCII-compatible. */ public static boolean verifyEscaped(char[] original) { for (int i = 0; i < original.length; i++) { int c = original[i]; if (c > 128) { return false; } else if (c == '%') { if (Character.digit(original[++i], 16) == -1 || Character.digit(original[++i], 16) == -1) { return false; } } } return true; } /** * Replace from a given character to given character in an array order * for a given string. * * @param original a given string * @param from a replacing character array * @param to a replaced character array * @return the replaced string */ public static String replace(String original, char[] from, char[] to) { for (int i = from.length; i > 0; --i) { original = replace(original, from[i], to[i]); } return original; } /** * Replace from a given character to given character for a given string. * * @param original a given string * @param from a replacing character array * @param to a replaced character array * @return the replaced string */ public static String replace(String original, char from, char to) { StringBuffer result = new StringBuffer(original.length()); int at, saved = 0; do { at = original.indexOf(from); if (at >= 0) { result.append(original.substring(0, at)); result.append(to); } else { result.append(original.substring(saved)); } saved = at; } while (at >= 0); return result.toString(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -