decodeBytes static method

String decodeBytes(
  1. Uint8List bytes, {
  2. String? charset,
})

Implementation

static String decodeBytes(Uint8List bytes, {String? charset}) {
  final String name = (charset ?? "").toLowerCase().replaceAll("_", "-");
  if (name.contains("1256") || name.contains("arabic")) return Cp1256.decode(bytes);
  if (name.contains("latin") || name.contains("8859-1")) return latin1.decode(bytes, allowInvalid: true);
  if (bytes.length >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF) return _decodeUtf16(bytes, 2, bigEndian: true);
  if (bytes.length >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE) return _decodeUtf16(bytes, 2, bigEndian: false);
  final int start = bytes.length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF ? 3 : 0;
  try {
    return utf8.decode(start == 0 ? bytes : Uint8List.sublistView(bytes, start));
  } on FormatException {
    return _looksLikeCp1256(bytes) ? Cp1256.decode(bytes) : latin1.decode(bytes, allowInvalid: true);
  }
}