detectEncoding function

String detectEncoding(
  1. Uint8List bytes
)

Implementation

String detectEncoding(Uint8List bytes) {
  if (bytes.length < 4) return 'unknown';

  // Check UTF-8 BOM
  if (bytes.length >= 3 &&
      bytes[0] == 0xEF &&
      bytes[1] == 0xBB &&
      bytes[2] == 0xBF) {
    return 'UTF-8';
  }

  // Check UTF-16 LE BOM
  if (bytes.length >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE) {
    return 'UTF-16LE';
  }

  // Check UTF-16 BE BOM
  if (bytes.length >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF) {
    return 'UTF-16BE';
  }

  // If no BOM is found, assume UTF-8
  return 'UTF-8';
}