List<int> contentAsBytes()

The content part of the data URI as bytes.

If the data is Base64 encoded, it will be decoded to bytes.

If the data is not Base64 encoded, it will be decoded by unescaping percent-escaped characters and returning byte values of each unescaped character. The bytes will not be, e.g., UTF-8 decoded.

Source

List<int> contentAsBytes() {
  String text = _text;
  int start = _separatorIndices.last + 1;
  if (isBase64) {
    return BASE64.decoder.convert(text, start);
  }

  // Not base64, do percent-decoding and return the remaining bytes.
  // Compute result size.
  const int percent = 0x25;
  int length = text.length - start;
  for (int i = start; i < text.length; i++) {
    var codeUnit = text.codeUnitAt(i);
    if (codeUnit == percent) {
      i += 2;
      length -= 2;
    }
  }
  // Fill result array.
  Uint8List result = new Uint8List(length);
  if (length == text.length) {
    result.setRange(0, length, text.codeUnits, start);
    return result;
  }
  int index = 0;
  for (int i = start; i < text.length; i++) {
    var codeUnit = text.codeUnitAt(i);
    if (codeUnit != percent) {
      result[index++] = codeUnit;
    } else {
      if (i + 2 < text.length) {
        var digit1 = _Uri._parseHexDigit(text.codeUnitAt(i + 1));
        var digit2 = _Uri._parseHexDigit(text.codeUnitAt(i + 2));
        if (digit1 >= 0 && digit2 >= 0) {
          int byte = digit1 * 16 + digit2;
          result[index++] = byte;
          i += 2;
          continue;
        }
      }
      throw new FormatException("Invalid percent escape", text, i);
    }
  }
  assert(index == result.length);
  return result;
}