Package libcore.net

Class UriCodec


  • public abstract class UriCodec
    extends Object
    Encodes and decodes “application/x-www-form-urlencoded” content. Subclasses define “isRetained”, which decides which chars need to be escaped and which don’t. Output is encoded as UTF-8 by default. I.e, each character (or surrogate pair) is converted to its equivalent UTF-8 encoded byte sequence, which is then converted to it’s escaped form. e.g a 4 byte sequence might look like” %c6%ef%e0%e8”
    • Constructor Detail

      • UriCodec

        public UriCodec()
    • Method Detail

      • isRetained

        protected abstract boolean isRetained​(char c)
        Returns true iff. ‘c’ does not need to be escaped. 'a’ - ‘z’ , ‘A’ - ‘Z’ and ‘0’ - ‘9’ are always considered valid (i.e, don’t need to be escaped. This set is referred to as the ``whitelist''.
      • validate

        public final String validate​(String uri,
                                     int start,
                                     int end,
                                     String name)
                              throws URISyntaxException
        Throw URISyntaxException if any of the characters in the range [start, end) are not valid according to this codec. - If a char is in the whitelist or retained, it is valid both escaped and unescaped. - All escaped octets appearing in the input are structurally valid hex, i.e convertible to decimals. On success, the substring [start, end) is returned. name is not used, except to generate debugging info.
        Throws:
        URISyntaxException
      • encode

        public final String encode​(String s,
                                   Charset charset)
        Encodes the string s as per the rules of this encoder (see class level comment).
        Throws:
        IllegalArgumentException - if the encoder is unable to encode a sequence of bytes.
      • appendEncoded

        public final void appendEncoded​(StringBuilder builder,
                                        String s)
        Encodes the string s as per the rules of this encoder (see class level comment). Encoded output is appended to builder. This uses the default output encoding (UTF-8).
      • appendPartiallyEncoded

        public final void appendPartiallyEncoded​(StringBuilder builder,
                                                 String s)
        Encodes the string s as per the rules of this encoder (see class level comment). Encoded output is appended to builder. This uses the default output encoding (UTF-8). This method must produce partially encoded output. What this means is that if encoded octets appear in the input string, they are passed through unmodified, instead of being double escaped. Consider a decoder operating on the global whitelist dealing with a string “foo%25bar”. With this method, the output will be “foo%25bar”, but with appendEncoded, it will be double encoded into “foo%2525bar”.
      • decode

        public static String decode​(String s,
                                    boolean convertPlus,
                                    Charset charset,
                                    boolean throwOnFailure)
        Decode a string according to the rules of this decoder. - if convertPlus == true all ‘+’ chars in the decoded output are converted to ‘ ‘ (white space) - if throwOnFailure == true, an IllegalArgumentException is thrown for invalid inputs. Else, U+FFFd is emitted to the output in place of invalid input octets.
      • decode

        public static String decode​(String s)
        Equivalent to decode(s, false, UTF_8, true)