Package libcore.net
Class UriCodec
- java.lang.Object
-
- libcore.net.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 Summary
Constructors Constructor Description UriCodec()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidappendEncoded(StringBuilder builder, String s)Encodes the stringsas per the rules of this encoder (see class level comment).voidappendPartiallyEncoded(StringBuilder builder, String s)Encodes the stringsas per the rules of this encoder (see class level comment).static Stringdecode(String s)Equivalent todecode(s, false, UTF_8, true)static Stringdecode(String s, boolean convertPlus, Charset charset, boolean throwOnFailure)Decode a string according to the rules of this decoderStringencode(String s, Charset charset)Encodes the stringsas per the rules of this encoder (see class level comment).protected abstract booleanisRetained(char c)Returns true iff.Stringvalidate(String uri, int start, int end, String name)Throw URISyntaxException if any of the characters in the range [start, end) are not valid according to this codecstatic voidvalidateSimple(String uri, String legal)
-
-
-
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.nameis not used, except to generate debugging info.- Throws:
URISyntaxException
-
validateSimple
public static void validateSimple(String uri, String legal) throws URISyntaxException
- Throws:
URISyntaxException
-
encode
public final String encode(String s, Charset charset)
Encodes the stringsas 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 stringsas per the rules of this encoder (see class level comment). Encoded output is appended tobuilder. This uses the default output encoding (UTF-8).
-
appendPartiallyEncoded
public final void appendPartiallyEncoded(StringBuilder builder, String s)
Encodes the stringsas per the rules of this encoder (see class level comment). Encoded output is appended tobuilder. 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. - ifconvertPlus == trueall ‘+’ chars in the decoded output are converted to ‘ ‘ (white space) - ifthrowOnFailure == true, anIllegalArgumentExceptionis thrown for invalid inputs. Else, U+FFFd is emitted to the output in place of invalid input octets.
-
-