public class PercentEscaper extends UnicodeEscaper
UnicodeEscaper that escapes some set of Java characters using the
URI percent encoding scheme. The set of safe characters (those which remain
unescaped) can be specified on construction.
For details on escaping URIs for use in web pages, see section 2.4 of RFC 3986.
In most cases this class should not need to be used directly. If you have no special requirements for escaping your URIs, you should use either
When encoding a String, the following rules apply:
plusForSpace was specified, the space character " " is
converted into a plus sign "+".
RFC 2396 specifies the set of unreserved characters as "-", "_", ".", "!", "~", "*", "'", "(" and ")". It goes on to state:
Unreserved characters can be escaped without changing the semantics of the URI, but this should not be done unless the URI is being used in a context that does not allow the unescaped character to appear.
For performance reasons the only currently supported character encoding of this class is UTF-8.
Note: This escaper produces uppercase hexidecimal sequences. From RFC 3986:
"URI producers and normalizers should use uppercase hexadecimal digits for
all percent-encodings."
| Modifier and Type | Field and Description |
|---|---|
static String |
SAFECHARS_URLENCODER |
static String |
SAFEPATHCHARS_URLENCODER
A string of characters that do not need to be encoded when used in URI
path segments, as specified in RFC 3986.
|
static String |
SAFEQUERYSTRINGCHARS_URLENCODER
A string of characters that do not need to be encoded when used in URI
query strings, as specified in RFC 3986.
|
| Constructor and Description |
|---|
PercentEscaper(String safeChars,
boolean plusForSpace)
Constructs a URI escaper with the specified safe characters and optional
handling of the space character.
|
| Modifier and Type | Method and Description |
|---|---|
protected char[] |
escape(int cp)
Escapes the given Unicode code point in UTF-8.
|
String |
escape(String s)
Returns the escaped form of a given literal string.
|
protected int |
nextEscapeIndex(CharSequence csq,
int index,
int end) |
codePointAt, escape, escapeSlowpublic static final String SAFECHARS_URLENCODER
public static final String SAFEPATHCHARS_URLENCODER
public static final String SAFEQUERYSTRINGCHARS_URLENCODER
public PercentEscaper(String safeChars, boolean plusForSpace)
safeChars - a non null string specifying additional safe characters for
this escaper (the ranges 0..9, a..z and A..Z are always safe
and should not be specified here)plusForSpace - true if ASCII space should be escaped to + rather than
%20IllegalArgumentException - if any of the parameters were invalidprotected int nextEscapeIndex(CharSequence csq, int index, int end)
nextEscapeIndex in class UnicodeEscaperpublic String escape(String s)
EscaperNote that this method may treat input characters differently depending on the specific escaper implementation.
escape in interface Escaperescape in class UnicodeEscapers - the literal string to be escapedstringprotected char[] escape(int cp)
escape in class UnicodeEscapercp - the Unicode code point to escape if necessarynull if no escaping was
neededCopyright © 2008–2017. All rights reserved.