@Beta @GwtCompatible public final class PercentEscaper extends UnicodeEscaper
UnicodeEscaperthat 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:
plusForSpacewas 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.
|Constructor and Description|
Constructs a percent escaper with the specified safe characters and optional handling of the space character.
|Modifier and Type||Method and Description|
Escapes the given Unicode code point in UTF-8.
Returns the escaped form of a given literal string.
Scans a sub-sequence of characters from a given
public PercentEscaper(String safeChars, boolean plusForSpace)
Not that it is allowed, but not necessarily desirable to specify
% as a safe
character. This has the effect of creating an escaper which has no well defined inverse but it
can be useful when escaping additional characters.
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
IllegalArgumentException- if any of the parameters were invalid
protected int nextEscapeIndex(CharSequence csq, int index, int end)
CharSequence, returning the index of the next character that requires escaping.
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
UnicodeEscaper.escape(int) for each of them. If the semantics of your escaper are such that code
points in the supplementary range are either all escaped or all unescaped, this method can be
implemented more efficiently using
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.
PercentEscaper for an example.
csq- a sequence of characters
index- the index of the first character to be scanned
end- the index immediately after the last character to be scanned
public String escape(String s)
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
IllegalArgumentException. You should ensure your input is valid
UTF-16 before calling this method.
Note: When implementing an escaper it is a good idea to override this method for
efficiency by inlining the implementation of
UnicodeEscaper.nextEscapeIndex(CharSequence, int, int)
directly. Doing this for
PercentEscaper more than doubled the
performance for unescaped strings (as measured by
s- the literal string to be escaped
protected char escape(int cp)
cp- the Unicode code point to escape if necessary
nullif no escaping was needed
Copyright © 2010-2017. All Rights Reserved.