UriData parse(String uri)

Parses a string as a data URI.

The string must have the format:

'data:' (type '/' subtype)? (';' attribute '=' value)* (';base64')? ',' data

where type, subtype, attribute and value are specified in RFC-2045, and data is a sequence of URI-characters (RFC-2396 uric).

This means that all the characters must be ASCII, but the URI may contain percent-escapes for non-ASCII byte values that need an interpretation to be converted to the corresponding string.

Parsing doesn't check the validity of any part, it just checks that the input has the correct structure with the correct sequence of /, ;, = and , delimiters.

Accessing the individual parts may fail later if they turn out to have content that can't be decoded successfully as a string.

Source

static UriData parse(String uri) {
  if (uri.length >= 5) {
    int dataDelta = _startsWithData(uri, 0);
    if (dataDelta == 0) {
      // Exact match on "data:".
      return _parse(uri, 5, null);
    }
    if (dataDelta == 0x20) {
      // Starts with a non-normalized "data" scheme containing upper-case
      // letters. Parse anyway, but throw away the scheme.
      return _parse(uri.substring(5), 0, null);
    }
  }
  throw new FormatException("Does not start with 'data:'", uri, 0);
}