isBinaryStl function

bool isBinaryStl(
  1. Uint8List bytes
)

True when bytes is a binary STL file.

The one check that actually distinguishes the two dialects. An ASCII file always starts with solid, and so — misleadingly — does a binary one: the format's 80-byte header is free text, and most binary exporters write solid <name> into it out of habit. Reading a binary file as text on the strength of that prefix is a real, repeated bug in other STL readers. The only fact that cannot lie is the size: a binary file is exactly its header plus 50 bytes a facet, and an ASCII file — being text with a facet spelled out in words — never lands on that number by accident at any real triangle count.

Implementation

bool isBinaryStl(Uint8List bytes) {
  if (bytes.length < 84) return false;
  final count = ByteData.sublistView(bytes, 80, 84).getUint32(0, Endian.little);
  return bytes.length == 84 + 50 * count;
}