isBinaryContent function

bool isBinaryContent(
  1. Uint8List buffer
)

Detect binary content by checking for null bytes or >10% non-printable characters in the first binaryCheckSize bytes.

Implementation

bool isBinaryContent(Uint8List buffer) {
  final checkLength = buffer.length < binaryCheckSize
      ? buffer.length
      : binaryCheckSize;
  if (checkLength == 0) return false;

  int nonPrintable = 0;
  for (int i = 0; i < checkLength; i++) {
    final byte = buffer[i];
    // Null byte → definitely binary
    if (byte == 0) return true;
    // Non-printable: not tab (9), not newline (10), not carriage return (13),
    // and outside printable ASCII range (32-126)
    if (byte != 9 && byte != 10 && byte != 13 && (byte < 32 || byte > 126)) {
      nonPrintable++;
    }
  }

  return nonPrintable / checkLength > 0.1;
}