contentAsBytes 方法
数据URI的内容部分转换为字节。
如果数据是Base64编码的,它将被解码为字节。
如果数据不是Base64编码的,它将通过反转百分号转义字符来解码,并返回每个未转义字符的字节值。字节值不会被如UTF-8解码。
实现
Uint8List 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 = 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) {
int byte = parseHexByte(text, i + 1);
if (byte >= 0) {
result[index++] = byte;
i += 2;
continue;
}
}
throw FormatException("Invalid percent escape", text, i);
}
}
assert(index == result.length);
return result;
}