decodeAsText static method
Decodes the given data
Implementation
static String decodeAsText(
final Uint8List data,
final String? transferEncoding,
final String? charset,
) {
if (transferEncoding == null && charset == null) {
// this could be a) UTF-8 or b) UTF-16 most likely:
final utf8Decoded = encodingUtf8.decode(data, allowMalformed: true);
if (utf8Decoded.contains('�')) {
final comparison = String.fromCharCodes(data);
if (!comparison.contains('�')) {
return comparison;
}
}
return utf8Decoded;
}
// there is actually just one interesting case:
// when the transfer encoding is 8bit, the text needs to be decoded with
// the specified charset.
// Note that some mail senders also declare 7bit message encoding even when
// UTF8 or other 8bit encodings are used.
// In other cases the text is ASCII and the 'normal' decodeAnyText method
// can be used.
final transferEncodingLC = transferEncoding?.toLowerCase() ?? '8bit';
if (transferEncodingLC == '8bit' ||
transferEncodingLC == '7bit' ||
transferEncodingLC == 'binary') {
final cs = charset ?? 'utf8';
final codec = _charsetCodecsByName[cs.toLowerCase()]?.call();
if (codec == null) {
print('Error: no encoding found for charset [$cs].');
return encodingUtf8.decode(data, allowMalformed: true);
}
final decodedText = codec.decode(data);
return decodedText;
}
final text = String.fromCharCodes(data);
return decodeAnyText(text, transferEncoding, charset);
}