UnicodeEscaper that escapes some set of Java characters using a UTF-8 based percent
encoding scheme. The set of safe characters (those which remain unescaped) can be specified on
construction.
This class is primarily used for creating URI escapers in UrlEscapers but can be used
directly if required. While URI escapers impose specific semantics on which characters are
considered 'safe', this class has a minimal set of restrictions.
When escaping a String, the following rules apply:
plusForSpace was specified, the space character " " is converted into a plus
sign "+".
For performance reasons the only currently supported character encoding of this class is UTF-8.
Note
This escaper produces uppercase hexadecimal sequences.
| Modifier and Type | Field and Description |
|---|---|
| private static final char[] | |
| private final boolean | plusForSpace
If true we should convert space to the |
| private final boolean[] | safeOctets
An array of flags where for any |
| 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 percent 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 array with entries corresponding to the character values specified in safeChars set to true. |
| public String | |
| protected char[] | escape(int
the Unicode code point to escape if necessary cp)Implements abstract com. |
| 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 com. CharSequence, returning the index of
the next character that requires escaping.
|
| PLUS_SIGN | back to summary |
|---|---|
| private static final char[] PLUS_SIGN | |
| plusForSpace | back to summary |
|---|---|
| private final boolean plusForSpace If true we should convert space to the | |
| safeOctets | back to summary |
|---|---|
| private final boolean[] safeOctets An array of flags where for any | |
| UPPER_HEX_DIGITS | back to summary |
|---|---|
| private static final char[] UPPER_HEX_DIGITS | |
| PercentEscaper | back to summary |
|---|---|
| public PercentEscaper(String safeChars, boolean plusForSpace) Constructs a percent escaper with the specified safe characters and optional handling of the space character. Not that it is allowed, but not necessarily desirable to specify
| |
| createSafeOctets | back to summary |
|---|---|
| private static boolean[] createSafeOctets(String safeChars) Creates a boolean array with entries corresponding to the character values 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 com. Doc from com. 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 com. Escapes the given Unicode code point in UTF-8.
| |
| nextEscapeIndex | back to summary |
|---|---|
| protected int nextEscapeIndex(CharSequence csq, int index, int end) Overrides com. Doc from com. 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
| |