Top Description Fields Constructors Methods
io.opentelemetry.api.internal

public final Class PercentEscaper

extends Object
Class Inheritance
Imports
javax.annotation.CheckForNull

Note: This class is based on code from guava. It is comprised of code from three classes:

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.

This class is internal and is hence not for public use. Its APIs are unstable and can change at any time.

Author
David Beaumont
Since
15.0

Field Summary

Modifier and TypeField and Description
private static final int
DEST_PAD

The amount of padding (chars) to use when growing the escape buffer.

private static final String
private static 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

Method Summary

Modifier and TypeMethod and Description
private static int

Returns:

the Unicode code point for the given index or the negated value of the trailing high surrogate character at the end of the sequence
codePointAt
(CharSequence
the sequence of characters from which to decode the code point
seq
,
int
the index of the first character to decode
index
,
int
the index beyond the last valid character to decode
end
)

Returns the Unicode code point of the character at the given index.

public static PercentEscaper
create()

The default PercentEscaper which will *not* replace spaces with plus signs.

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 s)

Escape the provided String, using percent-style URL Encoding.

private static char[]
escape(int cp)

Escapes the given Unicode code point in UTF-8.

private static String

Returns:

the escaped form of string
escapeSlow
(String
the literal string to be escaped
s
,
int
the index to start escaping from
index
)

Returns the escaped form of a given literal string, starting at the given index.

private static char[]
growBuffer(char[] dest, int index, int size)

Helper method to grow the character buffer as needed, this only happens once in a while so it's ok if it's in a method call.

private static int
nextEscapeIndex(CharSequence csq, int index, int end)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEST_PADback to summary
private static final int DEST_PAD

The amount of padding (chars) to use when growing the escape buffer.

SAFE_CHARSback to summary
private static final String SAFE_CHARS
safeOctetsback to summary
private static 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()

Method Detail

codePointAtback to summary
private static int codePointAt(CharSequence seq, int index, int end)

Returns the Unicode code point of the character at the given index.

Unlike Character#codePointAt(CharSequence, int) or String#codePointAt(int) this method will never fail silently when encountering an invalid surrogate pair.

The behaviour of this method is as follows:

  1. If index >= end, IndexOutOfBoundsException is thrown.
  2. If the character at the specified index is not a surrogate, it is returned.
  3. If the first character was a high surrogate value, then an attempt is made to read the next character.
    1. If the end of the sequence was reached, the negated value of the trailing high surrogate is returned.
    2. If the next character was a valid low surrogate, the code point value of the high/low surrogate pair is returned.
    3. If the next character was not a low surrogate value, then IllegalArgumentException is thrown.
  4. If the first character was a low surrogate value, IllegalArgumentException is thrown.
Parameters
seq:CharSequence

the sequence of characters from which to decode the code point

index:int

the index of the first character to decode

end:int

the index beyond the last valid character to decode

Returns:int

the Unicode code point for the given index or the negated value of the trailing high surrogate character at the end of the sequence

createback to summary
public static PercentEscaper create()

The default PercentEscaper which will *not* replace spaces with plus signs.

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)

Escape the provided String, using percent-style URL Encoding.

escapeback to summary
private static char[] escape(int cp)

Escapes the given Unicode code point in UTF-8.

Annotations
@CheckForNull
@SuppressWarnings:UngroupedOverloads
escapeSlowback to summary
private static String escapeSlow(String s, int index)

Returns the escaped form of a given literal string, starting at the given index. This method is called by the escape(String) method when it discovers that escaping is required. It is protected to allow subclasses to override the fastpath escaping function to inline their escaping test.

This method is not reentrant and may only be invoked by the top level escape(String) method.

Parameters
s:String

the literal string to be escaped

index:int

the index to start escaping from

Returns:String

the escaped form of string

Exceptions
NullPointerException:
if string is null
IllegalArgumentException:
if invalid surrogate characters are encountered
growBufferback to summary
private static char[] growBuffer(char[] dest, int index, int size)

Helper method to grow the character buffer as needed, this only happens once in a while so it's ok if it's in a method call. If the index passed in is 0 then no copying will be done.

nextEscapeIndexback to summary
private static int nextEscapeIndex(CharSequence csq, int index, int end)