Top Description Fields Constructors Methods
com.google.common.net

public final Class PercentEscaper

extends UnicodeEscaper
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull

A 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:

For performance reasons the only currently supported character encoding of this class is UTF-8.

Note

This escaper produces uppercase hexadecimal sequences.

Author
David Beaumont
Since
15.0

Field Summary

Modifier and TypeField and Description
private static final char[]
private final boolean
plusForSpace

If true we should convert space to the + character.

private final boolean[]
safeOctets

An array of flags where for any char c if safeOctets[c] is true then c should remain unmodified in the output.

private static final char[]

Constructor Summary

AccessConstructor 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 + rather than %20
plusForSpace
)

Constructs a percent escaper with the specified safe characters and optional handling of the space character.

Method Summary

Modifier and TypeMethod 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
escape(String
the literal string to be escaped
s
)

Overrides com.google.common.escape.UnicodeEscaper.escape.

Returns the escaped form of a given literal string.
protected char[]
escape(int
the Unicode code point to escape if necessary
cp
)

Implements abstract com.google.common.escape.UnicodeEscaper.escape.

Escapes the given Unicode code point in UTF-8.
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.google.common.escape.UnicodeEscaper.nextEscapeIndex.

Scans a sub-sequence of characters from a given CharSequence, returning the index of the next character that requires escaping.
Inherited from com.google.common.escape.UnicodeEscaper:
codePointAtescapeSlow

Field Detail

PLUS_SIGNback to summary
private static final char[] PLUS_SIGN
plusForSpaceback to summary
private final boolean plusForSpace

If true we should convert space to the + character.

safeOctetsback to summary
private final boolean[] safeOctets

An array of flags where for any char c if safeOctets[c] is true then c should remain unmodified in the output. If c >= safeOctets.length then it should be escaped.

UPPER_HEX_DIGITSback to summary
private static final char[] UPPER_HEX_DIGITS

Constructor Detail

PercentEscaperback 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 % 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.

Parameters
safeChars: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)

plusForSpace:boolean

true if ASCII space should be escaped to + rather than %20

Exceptions
IllegalArgumentException:
if any of the parameters were invalid

Method Detail

createSafeOctetsback 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.

escapeback to summary
public String escape(String s)

Overrides com.google.common.escape.UnicodeEscaper.escape.

Doc from com.google.common.escape.UnicodeEscaper.escape.

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 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 nextEscapeIndex(CharSequence, int, int) directly. Doing this for com.google.common.net.PercentEscaper more than doubled the performance for unescaped strings (as measured by CharEscapersBenchmark).

Parameters
s:String

the literal string to be escaped

Returns:String

the escaped form of string

Annotations
@Override
escapeback to summary
protected char[] escape(int cp)

Implements abstract com.google.common.escape.UnicodeEscaper.escape.

Escapes the given Unicode code point in UTF-8.

Parameters
cp:int

Doc from com.google.common.escape.UnicodeEscaper.escape.

the Unicode code point to escape if necessary

Returns:char[]

Doc from com.google.common.escape.UnicodeEscaper.escape.

the replacement characters, or null if no escaping was needed

Annotations
@Override
@CheckForNull
nextEscapeIndexback to summary
protected int nextEscapeIndex(CharSequence csq, int index, int end)

Overrides com.google.common.escape.UnicodeEscaper.nextEscapeIndex.

Doc from com.google.common.escape.UnicodeEscaper.nextEscapeIndex.

Scans a sub-sequence of characters from a given 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 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 CharSequence#charAt(int).

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 com.google.common.net.PercentEscaper for an example.

Parameters
csq:CharSequence

a sequence of characters

index:int

the index of the first character to be scanned

end:int

the index immediately after the last character to be scanned

Annotations
@Override