diff --git a/GenericStl/AsciiStlReader.cs b/GenericStl/AsciiStlReader.cs index c5e8a7f..fc4a73f 100644 --- a/GenericStl/AsciiStlReader.cs +++ b/GenericStl/AsciiStlReader.cs @@ -24,7 +24,7 @@ public AsciiStlReader(Func create public AsciiStlReader(IDataStructureCreator structureCreator) : base(structureCreator) - { + { } public IEnumerable Read(IEnumerable fileContent) @@ -55,12 +55,10 @@ public override IEnumerable ReadFromStream(Stream s) private static IEnumerable ReadLines(Stream s) { - using (var r = new StreamReader(s, Encoding.UTF8, true, DefaultBufferSize, true)) + var r = new StreamReader(s, Encoding.UTF8, true, DefaultBufferSize); // do not dispose this reader as it would dispose the stream + while (!r.EndOfStream) { - while (!r.EndOfStream) - { - yield return r.ReadLine(); - } + yield return r.ReadLine(); } } diff --git a/GenericStl/AsciiStlWriter.cs b/GenericStl/AsciiStlWriter.cs index d22c349..d126d73 100644 --- a/GenericStl/AsciiStlWriter.cs +++ b/GenericStl/AsciiStlWriter.cs @@ -11,7 +11,7 @@ namespace GenericStl [SuppressMessage("Microsoft.Design", "CA1005:AvoidExcessiveParametersOnGenericTypes")] public class AsciiStlWriter : StlWriterBase { - public AsciiStlWriter(Func> extractTriangle, Func> extractVertex, Func> extractNormal) + public AsciiStlWriter(Func> extractTriangle, Func> extractVertex, Func> extractNormal) : base(extractTriangle, extractVertex, extractNormal) { } @@ -19,7 +19,6 @@ public AsciiStlWriter(Func> public AsciiStlWriter(IDataStructureExtractor extractor) : base(extractor) { - } public override void WriteToFile(string fileName, IEnumerable triangles) @@ -47,10 +46,9 @@ public override void WriteToStream(Stream s, IEnumerable triangles) throw new ArgumentNullException("triangles"); } - using (var w = new StreamWriter(s, new UTF8Encoding(false, true), 1024, true)) - { - WriteTo(w, triangles); - } + var w = new StreamWriter(s, new UTF8Encoding(false, true), 1024); // do not dispose this reader as it would dispose the stream + WriteTo(w, triangles); + w.Flush(); } public string Write(IEnumerable triangles) diff --git a/GenericStl/BinaryStlReader.cs b/GenericStl/BinaryStlReader.cs index 366a8e1..5cc93b4 100644 --- a/GenericStl/BinaryStlReader.cs +++ b/GenericStl/BinaryStlReader.cs @@ -8,15 +8,14 @@ namespace GenericStl { public class BinaryStlReader : StlReaderBase { - public BinaryStlReader(Func createTriangle, Func createVertex, Func createNormal) + public BinaryStlReader(Func createTriangle, Func createVertex, Func createNormal) : base(createTriangle, createVertex, createNormal) { - } public BinaryStlReader(IDataStructureCreator structureCreator) : base(structureCreator) - { + { } public override IEnumerable ReadFromFile(string fileName) @@ -34,19 +33,18 @@ public override IEnumerable ReadFromStream(Stream s) throw new ArgumentNullException("s"); } - using (var reader = new BinaryReader(s, Encoding.UTF8, true)) - { - reader.ReadBytes(80); //header + var reader = new BinaryReader(s, Encoding.UTF8); // do not dispose this reader as it would dispose the stream - var numTriangles = reader.ReadInt32(); + reader.ReadBytes(80); //header - for (var i = 0; i < numTriangles; ++i) - { - yield return ReadTriangle(reader); - } + var numTriangles = reader.ReadInt32(); - Debug.Assert(s.Position == s.Length); + for (var i = 0; i < numTriangles; ++i) + { + yield return ReadTriangle(reader); } + + Debug.Assert(s.Position == s.Length); } @@ -78,7 +76,7 @@ public IEnumerable Read(byte[] data) using (var s = new MemoryStream(data, false)) { foreach (var triangle in ReadFromStream(s)) yield return triangle; - } + } } } -} +} \ No newline at end of file diff --git a/GenericStl/BinaryStlWriter.cs b/GenericStl/BinaryStlWriter.cs index 2a955bd..2223287 100644 --- a/GenericStl/BinaryStlWriter.cs +++ b/GenericStl/BinaryStlWriter.cs @@ -62,23 +62,22 @@ public void WriteToStream(Stream s, IEnumerable triangles, byte[] hea var hdr = PrepareHeader(header); - using (var w = new BinaryWriter(s, new UTF8Encoding(false, true), true)) - { - WriteHeader(w, hdr); - WriteLength(w, 0); - - var length = 0; + var w = new BinaryWriter(s, new UTF8Encoding(false, true)); // do not dispose this reader as it would dispose the stream + WriteHeader(w, hdr); + WriteLength(w, 0); - foreach (var triangle in triangles) - { - length++; + var length = 0; - WriteTriangle(w, triangle); - } + foreach (var triangle in triangles) + { + length++; - s.Seek(HeaderLengthInByte, SeekOrigin.Begin); - WriteLength(w, length); + WriteTriangle(w, triangle); } + + s.Seek(HeaderLengthInByte, SeekOrigin.Begin); + WriteLength(w, length); + w.Flush(); } private static byte[] PrepareHeader(byte[] header) @@ -97,7 +96,7 @@ private static byte[] PrepareHeader(byte[] header) if (header.Length < HeaderLengthInByte) { - Array.Copy(header, newHdr, header.Length); + Array.Copy(header, newHdr, header.Length); } else if (header.Length > HeaderLengthInByte) { diff --git a/GenericStl/GenericStl.csproj b/GenericStl/GenericStl.csproj index 0ae5d56..546c41c 100644 --- a/GenericStl/GenericStl.csproj +++ b/GenericStl/GenericStl.csproj @@ -9,11 +9,12 @@ Properties GenericStl GenericStl - v4.5 + v4.0 512 ..\ true true + true diff --git a/GenericStl/StlFile.cs b/GenericStl/StlFile.cs index 265fff6..c7aece8 100644 --- a/GenericStl/StlFile.cs +++ b/GenericStl/StlFile.cs @@ -23,24 +23,23 @@ public static bool IsBinary(Stream stream) { try { - using(var r = new BinaryReader(stream, Encoding.UTF8, true)) - { - var firstChars = new string(r.ReadChars(5)); + var r = new BinaryReader(stream, Encoding.UTF8); // do not dispose this reader as it would dispose the stream - if (!string.Equals(firstChars, "solid", StringComparison.OrdinalIgnoreCase)) - { - return true; - } + var firstChars = new string(r.ReadChars(5)); - var numberOfCharsToReadAtEnd = "endsolid".Length + 300; - numberOfCharsToReadAtEnd = numberOfCharsToReadAtEnd > stream.Length ? (int)stream.Length : numberOfCharsToReadAtEnd; + if (!string.Equals(firstChars, "solid", StringComparison.OrdinalIgnoreCase)) + { + return true; + } - stream.Seek(-numberOfCharsToReadAtEnd , SeekOrigin.End); + var numberOfCharsToReadAtEnd = "endsolid".Length + 300; + numberOfCharsToReadAtEnd = numberOfCharsToReadAtEnd > stream.Length ? (int) stream.Length : numberOfCharsToReadAtEnd; - var lastChars = new string(r.ReadChars(numberOfCharsToReadAtEnd)); + stream.Seek(-numberOfCharsToReadAtEnd, SeekOrigin.End); - return !lastChars.Contains("endsolid"); - } + var lastChars = new string(r.ReadChars(numberOfCharsToReadAtEnd)); + + return !lastChars.Contains("endsolid"); } finally {