diff --git a/src/STLDocument.cs b/src/STLDocument.cs index db9d317..c5b729c 100644 --- a/src/STLDocument.cs +++ b/src/STLDocument.cs @@ -125,22 +125,7 @@ public void AppendFacets(IEnumerable facets) /// True if the is text-based, otherwise false. public static bool IsText(Stream stream) { - if (stream == null) throw new NullReferenceException(nameof(stream)); - - const string solid = "solid"; - - byte[] buffer = new byte[5]; - string header; - - // Reset the stream to tbe beginning and read the first few bytes, then reset the stream to the beginning again. - stream.Seek(0, SeekOrigin.Begin); - stream.Read(buffer, 0, buffer.Length); - stream.Seek(0, SeekOrigin.Begin); - - // Read the header as ASCII. - header = Encoding.ASCII.GetString(buffer); - - return solid.Equals(header, StringComparison.InvariantCultureIgnoreCase); + return !IsBinary(stream); } /// Determines if the contained within the is binary-based. @@ -149,7 +134,18 @@ public static bool IsText(Stream stream) /// True if the is binary-based, otherwise false. public static bool IsBinary(Stream stream) { - return !IsText(stream); + if (stream == null) throw new NullReferenceException(nameof(stream)); + + using var reader = new BinaryReader(stream, Encoding.Default, true); + + stream.Seek(80, SeekOrigin.Begin); + + const numTriangles = reader.ReadUInt32(); + const expectedSize = 84 + numTriangles * 50; + + stream.Seek(0, SeekOrigin.Begin); + + return stream.Length == expectedSize; } /// Reads the contained within the into a new . diff --git a/tests/Data/BinaryWithSolidHeader.stl b/tests/Data/BinaryWithSolidHeader.stl new file mode 100644 index 0000000..2b65746 Binary files /dev/null and b/tests/Data/BinaryWithSolidHeader.stl differ diff --git a/tests/STLDocumentTests.cs b/tests/STLDocumentTests.cs index f6692d9..93e9e97 100644 --- a/tests/STLDocumentTests.cs +++ b/tests/STLDocumentTests.cs @@ -33,6 +33,19 @@ public void Read__FromBinaryStream() ValidateSTL(stlBinary); } + [Fact] + public void Read__FromBinaryStreamWithSolidHeader() + { + STLDocument stlBinary; + + using (var stream = GetData("BinaryWithSolidHeader.stl")) + { + stlBinary = STLDocument.Read(stream); + } + + ValidateSTL(stlBinary); + } + [Fact] public void Read__FromTextReader() {