Class PercentEscaper

java.lang.Object
dev.mccue.guava.escape.Escaper
dev.mccue.guava.escape.UnicodeEscaper
dev.mccue.guava.net.PercentEscaper

public final class PercentEscaper extends dev.mccue.guava.escape.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.

Since:
15.0
Author:
David Beaumont
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    protected char[]
    escape(int cp)
    Escapes the given Unicode code point in UTF-8.
     
    protected int
    nextEscapeIndex(CharSequence csq, int index, int end)
     

    Methods inherited from class dev.mccue.guava.escape.UnicodeEscaper

    codePointAt, escapeSlow

    Methods inherited from class dev.mccue.guava.escape.Escaper

    asFunction

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PercentEscaper

      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 - 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:
      IllegalArgumentException - if any of the parameters were invalid
  • Method Details

    • nextEscapeIndex

      protected int nextEscapeIndex(CharSequence csq, int index, int end)
      Overrides:
      nextEscapeIndex in class dev.mccue.guava.escape.UnicodeEscaper
    • escape

      public String escape(String s)
      Overrides:
      escape in class dev.mccue.guava.escape.UnicodeEscaper
    • escape

      @CheckForNull protected char[] escape(int cp)
      Escapes the given Unicode code point in UTF-8.
      Specified by:
      escape in class dev.mccue.guava.escape.UnicodeEscaper