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;
}