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 CharEscapers#uriEscaper() or
CharEscapers#uriEscaper(boolean).
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 |
|---|---|
| private final boolean | plusForSpace
If true we should convert space to the |
| public static final String | SAFECHARS_URLENCODER
A string of safe characters that mimics the behavior of |
| private final boolean[] | safeOctets
An array of flags where for any |
| public static final 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. |
| public static final 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. |
| private static final char[] | |
| private static final char[] |
| Access | Constructor and Description |
|---|---|
| public | PercentEscaper(String
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) safeChars, boolean true if ASCII space should be escaped to plusForSpace)+ rather than %20Constructs a URI escaper with the specified safe characters and optional handling of the space character. |
| Modifier and Type | Method and Description |
|---|---|
| private static boolean[] | createSafeOctets(String safeChars)
Creates a boolean[] with entries corresponding to the character values for 0-9, A-Z, a-z and those specified in safeChars set to true. |
| public String | escape(String
the literal string to be escaped s)Overrides org. Implements org. |
| protected char[] | escape(int
the Unicode code point to escape if necessary cp)Implements abstract org. |
| protected int | nextEscapeIndex(CharSequence
a sequence of characters csq, int the index of the first character to be scanned index, int the index immediately after the last character to be scanned end)Overrides org. CharSequence, returning the index of
the next character that requires escaping.
|
| plusForSpace | back to summary |
|---|---|
| private final boolean plusForSpace If true we should convert space to the | |
| SAFECHARS_URLENCODER | back to summary |
|---|---|
| public static final String SAFECHARS_URLENCODER A string of safe characters that mimics the behavior of | |
| safeOctets | back to summary |
|---|---|
| private final boolean[] safeOctets An array of flags where for any | |
| SAFEPATHCHARS_URLENCODER | back to summary |
|---|---|
| public static final 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. Note that some of these characters do need to be escaped when used in other parts of the URI. | |
| SAFEQUERYSTRINGCHARS_URLENCODER | back to summary |
|---|---|
| public static final 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. Note that some of these characters do need to be escaped when used in other parts of the URI. | |
| UPPER_HEX_DIGITS | back to summary |
|---|---|
| private static final char[] UPPER_HEX_DIGITS | |
| URI_ESCAPED_SPACE | back to summary |
|---|---|
| private static final char[] URI_ESCAPED_SPACE | |
| PercentEscaper | back to summary |
|---|---|
| public PercentEscaper(String safeChars, boolean plusForSpace) Constructs a URI escaper with the specified safe characters and optional handling of the space character.
| |
| createSafeOctets | back to summary |
|---|---|
| private static boolean[] createSafeOctets(String safeChars) Creates a boolean[] with entries corresponding to the character values for 0-9, A-Z, a-z and those specified in safeChars set to true. The array is as small as is required to hold the given character information. | |
| escape | back to summary |
|---|---|
| public String escape(String s) Overrides org. Implements org. Doc from org. Returns the escaped form of a given literal string.
If you are escaping input in arbitrary successive chunks, then it is not generally safe to use
this method. If an input string ends with an unmatched high surrogate character, then this
method will throw Note When implementing an escaper it is a good idea to override this method for
efficiency by inlining the implementation of | |
| escape | back to summary |
|---|---|
| protected char[] escape(int cp) Implements abstract org. Escapes the given Unicode code point in UTF-8.
| |
| nextEscapeIndex | back to summary |
|---|---|
| protected int nextEscapeIndex(CharSequence csq, int index, int end) Overrides org. Doc from org. Scans a sub-sequence of characters from a given Note When implementing an escaper, it is a good idea to override this method for
efficiency. The base class implementation determines successive Unicode code points and invokes
Note however that if your escaper does not escape characters in the supplementary range, you should either continue to validate the correctness of any surrogate characters encountered or provide a clear warning to users that your escaper does not validate its input.
See
| |