Class PercentEscaper


  • @Beta
    @GwtCompatible
    public final class PercentEscaper
    extends UnicodeEscaper
    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:

    • All specified safe characters remain unchanged.
    • If plusForSpace was specified, the space character " " is converted into a plus sign "+".
    • All other characters are converted into one or more bytes using UTF-8 encoding and each byte is then represented by the 3-character string "%XX", where "XX" is the two-digit, uppercase, hexadecimal representation of the byte value.

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

    Note: This escaper produces uppercase hexadecimal sequences. From RFC 3986:
    "URI producers and normalizers should use uppercase hexadecimal digits for all percent-encodings."

    Since:
    15.0
    • Constructor Summary

      Constructors 
      Constructor Description
      PercentEscaper​(java.lang.String safeChars, boolean plusForSpace)
      Constructs a percent escaper with the specified safe characters and optional handling of the space character.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String escape​(java.lang.String s)
      Returns the escaped form of a given literal string.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PercentEscaper

        public PercentEscaper​(java.lang.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 - 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 + rather than %20
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters were invalid
    • Method Detail

      • escape

        public java.lang.String escape​(java.lang.String s)
        Description copied from class: UnicodeEscaper
        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 UnicodeEscaper.nextEscapeIndex(CharSequence, int, int) directly. Doing this for PercentEscaper more than doubled the performance for unescaped strings (as measured by CharEscapersBenchmark).

        Overrides:
        escape in class UnicodeEscaper
        Parameters:
        s - the literal string to be escaped
        Returns:
        the escaped form of string