From ada21dcf3802d5e1daa9b67bca4e9daaabb838de Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sat, 14 Nov 2026 14:14:23 +0100 Subject: [PATCH] Stream to File: port to .NET 11, fix benchmark setup, add CopyToFileAsync Both projects target net11.0. BenchmarkDotNet 0.15.8 with [InProcess]; the benchmark loads its input in [GlobalSetup] instead of reading statics set in Program.cs, so it measures the real image. Adds CopyToFileAsync with a test and a benchmark, a CopyTo position demo in Program.cs, and tests that compare every written file with the source byte for byte. MSTest 4.5.3, Test.Sdk 18.12.0, coverlet 10.1.0. --- .../HowToConvertAStreamToAFileInCSharp.cs | 7 ++ .../HowToConvertAStreamToAFileInCSharp.csproj | 4 +- ...oConvertAStreamToAFileInCSharpBenchmark.cs | 35 +++++-- .../Program.cs | 42 ++++---- .../ConvertAStreamToAFileInCSharpTests.cs | 96 ++++++++++++------- .../Tests/Tests.csproj | 10 +- 6 files changed, 126 insertions(+), 68 deletions(-) diff --git a/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp.cs b/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp.cs index 67a7c51ecd..e1afa65ac4 100644 --- a/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp.cs +++ b/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp.cs @@ -9,6 +9,13 @@ public static void CopyToFile(MemoryStream stream, string path) stream.CopyTo(fileStream); } + public static async Task CopyToFileAsync(MemoryStream stream, string path) + { + await using var fileStream = File.Create(path); + stream.Position = 0; + await stream.CopyToAsync(fileStream); + } + public static void WriteToFileStream(MemoryStream stream, string path) { using var fileStream = File.Create(path); diff --git a/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp.csproj b/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp.csproj index 9a29e7e4a1..c2d3338a61 100644 --- a/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp.csproj +++ b/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp.csproj @@ -2,13 +2,13 @@ Exe - net8.0 + net11.0 enable enable - + diff --git a/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharpBenchmark.cs b/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharpBenchmark.cs index 82fc1b6a56..a718e98b8a 100644 --- a/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharpBenchmark.cs +++ b/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharpBenchmark.cs @@ -1,36 +1,55 @@ using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Order; namespace HowToConvertAStreamToAFileInCSharp; +[InProcess] [MemoryDiagnoser] -[Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.FastestToSlowest)] +[Orderer(SummaryOrderPolicy.FastestToSlowest)] public class ConvertAStreamToAFileInCSharpBenchmark { - public static MemoryStream Stream { get; set; } = new MemoryStream(); + private readonly MemoryStream _stream = new(); + private string _destinationPath = string.Empty; - public static string DestinationPath { get; set; } = string.Empty; + [GlobalSetup] + public void Setup() + { + _destinationPath = Path.Combine(AppContext.BaseDirectory, "Files"); + + using var fileStream = File.OpenRead(Path.Combine(_destinationPath, "source.png")); + fileStream.CopyTo(_stream); + } + + [GlobalCleanup] + public void Cleanup() => _stream.Dispose(); [Benchmark] public void CopyToBenchmark() { - ConvertAStreamToAFileInCSharp.CopyToFile(Stream, Path.Combine([DestinationPath, "CopyTo.png"])); + ConvertAStreamToAFileInCSharp.CopyToFile(_stream, Path.Combine(_destinationPath, "CopyTo.png")); + } + + [Benchmark] + public async Task CopyToAsyncBenchmark() + { + await ConvertAStreamToAFileInCSharp.CopyToFileAsync(_stream, Path.Combine(_destinationPath, "CopyToAsync.png")); } [Benchmark] public void WriteBenchmark() { - ConvertAStreamToAFileInCSharp.WriteToFileStream(Stream, Path.Combine([DestinationPath, "Write.png"])); + ConvertAStreamToAFileInCSharp.WriteToFileStream(_stream, Path.Combine(_destinationPath, "Write.png")); } [Benchmark] public void WriteByteBenchmark() { - ConvertAStreamToAFileInCSharp.WriteByteToFileStream(Stream, Path.Combine([DestinationPath, "WriteByte.png"])); + ConvertAStreamToAFileInCSharp.WriteByteToFileStream(_stream, Path.Combine(_destinationPath, "WriteByte.png")); } [Benchmark] public void WriteAllBytesBenchmark() { - ConvertAStreamToAFileInCSharp.WriteAllBytesFile(Stream, Path.Combine([DestinationPath, "WriteAllBytes.png"])); + ConvertAStreamToAFileInCSharp.WriteAllBytesFile(_stream, Path.Combine(_destinationPath, "WriteAllBytes.png")); } -} \ No newline at end of file +} diff --git a/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/Program.cs b/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/Program.cs index 548253c86e..37a1437265 100644 --- a/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/Program.cs +++ b/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/HowToConvertAStreamToAFileInCSharp/Program.cs @@ -1,26 +1,34 @@ -using System.Reflection; -using BenchmarkDotNet.Running; +using BenchmarkDotNet.Running; using HowToConvertAStreamToAFileInCSharp; -var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); -if (!Directory.Exists(directory)) -{ - return; -} - -var sourceFile = Path.Combine([directory, "Files", "source.png"]); -var destinationPath = Path.Combine([directory, "Files"]); +var directory = AppContext.BaseDirectory; +var sourceFile = Path.Combine(directory, "Files", "source.png"); +var destinationPath = Path.Combine(directory, "Files"); using var fileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read); using var memoryStream = new MemoryStream(); fileStream.CopyTo(memoryStream); -ConvertAStreamToAFileInCSharp.CopyToFile(memoryStream, Path.Combine([destinationPath, "CopyTo.png"])); -ConvertAStreamToAFileInCSharp.WriteToFileStream(memoryStream, Path.Combine([destinationPath, "WriteTo.png"])); -ConvertAStreamToAFileInCSharp.WriteByteToFileStream(memoryStream, Path.Combine([destinationPath, "WriteByte.png"])); -ConvertAStreamToAFileInCSharp.WriteAllBytesFile(memoryStream, Path.Combine([destinationPath, "WriteAllBytes.png"])); +Console.WriteLine($"Position after loading: {memoryStream.Position} of {memoryStream.Length}"); + +using (var noRewindFile = File.Create(Path.Combine(destinationPath, "NoRewind.png"))) +{ + memoryStream.CopyTo(noRewindFile); + Console.WriteLine($"Bytes written without rewinding: {noRewindFile.Length}"); +} + +memoryStream.Position = 0; +using (var rewindFile = File.Create(Path.Combine(destinationPath, "Rewind.png"))) +{ + memoryStream.CopyTo(rewindFile); + Console.WriteLine($"Bytes written after rewinding: {rewindFile.Length}"); + Console.WriteLine($"Position after CopyTo: source {memoryStream.Position}, destination {rewindFile.Position}"); +} -ConvertAStreamToAFileInCSharpBenchmark.Stream = memoryStream; -ConvertAStreamToAFileInCSharpBenchmark.DestinationPath = destinationPath; +ConvertAStreamToAFileInCSharp.CopyToFile(memoryStream, Path.Combine(destinationPath, "CopyTo.png")); +await ConvertAStreamToAFileInCSharp.CopyToFileAsync(memoryStream, Path.Combine(destinationPath, "CopyToAsync.png")); +ConvertAStreamToAFileInCSharp.WriteToFileStream(memoryStream, Path.Combine(destinationPath, "WriteTo.png")); +ConvertAStreamToAFileInCSharp.WriteByteToFileStream(memoryStream, Path.Combine(destinationPath, "WriteByte.png")); +ConvertAStreamToAFileInCSharp.WriteAllBytesFile(memoryStream, Path.Combine(destinationPath, "WriteAllBytes.png")); -BenchmarkRunner.Run(); +BenchmarkRunner.Run(args: args); diff --git a/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/Tests/ConvertAStreamToAFileInCSharpTests.cs b/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/Tests/ConvertAStreamToAFileInCSharpTests.cs index bb9158a2cb..19ec9bb5ce 100644 --- a/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/Tests/ConvertAStreamToAFileInCSharpTests.cs +++ b/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/Tests/ConvertAStreamToAFileInCSharpTests.cs @@ -1,4 +1,3 @@ -using System.Reflection; using HowToConvertAStreamToAFileInCSharp; namespace Tests; @@ -6,73 +5,98 @@ namespace Tests; [TestClass] public class ConvertAStreamToAFileInCSharpTests { + private static readonly string FilesPath = Path.Combine(AppContext.BaseDirectory, "Files"); + private static readonly string SourceFile = Path.Combine(FilesPath, "source.png"); + [TestMethod] - public void WhenCopyToFile_FileExists() + public void WhenCopyToFile_ThenFileHasTheSourceBytes() { - var destinationPath = GetPath(); - var memoryStream = GetMemoryStream(); - ConvertAStreamToAFileInCSharp.CopyToFile(memoryStream, Path.Combine([destinationPath, "CopyTo.png"])); + var path = Path.Combine(FilesPath, "CopyTo.png"); + using var memoryStream = GetMemoryStream(); + + ConvertAStreamToAFileInCSharp.CopyToFile(memoryStream, path); - Assert.IsTrue(File.Exists(Path.Combine([destinationPath, "CopyTo.png"]))); + CollectionAssert.AreEqual(File.ReadAllBytes(SourceFile), File.ReadAllBytes(path)); } [TestMethod] - public void WhenWriteToFile_FileExists() + public async Task WhenCopyToFileAsync_ThenFileHasTheSourceBytes() { - var destinationPath = GetPath(); - var memoryStream = GetMemoryStream(); - ConvertAStreamToAFileInCSharp.WriteToFileStream(memoryStream, Path.Combine([destinationPath, "WriteTo.png"])); + var path = Path.Combine(FilesPath, "CopyToAsync.png"); + using var memoryStream = GetMemoryStream(); + + await ConvertAStreamToAFileInCSharp.CopyToFileAsync(memoryStream, path); - Assert.IsTrue(File.Exists(Path.Combine([destinationPath, "WriteTo.png"]))); + CollectionAssert.AreEqual(File.ReadAllBytes(SourceFile), File.ReadAllBytes(path)); } [TestMethod] - public void WhenWriteByteToFile_FileExists() + public void WhenWriteToFile_ThenFileHasTheSourceBytes() { - var destinationPath = GetPath(); - var memoryStream = GetMemoryStream(); - ConvertAStreamToAFileInCSharp.WriteByteToFileStream(memoryStream, Path.Combine([destinationPath, "WriteByte.png"])); + var path = Path.Combine(FilesPath, "WriteTo.png"); + using var memoryStream = GetMemoryStream(); - Assert.IsTrue(File.Exists(Path.Combine([destinationPath, "WriteByte.png"]))); + ConvertAStreamToAFileInCSharp.WriteToFileStream(memoryStream, path); + + CollectionAssert.AreEqual(File.ReadAllBytes(SourceFile), File.ReadAllBytes(path)); } [TestMethod] - public void WhenWriteAllBytesToFile_FileExists() + public void WhenWriteByteToFile_ThenFileHasTheSourceBytes() { - var destinationPath = GetPath(); - var memoryStream = GetMemoryStream(); - ConvertAStreamToAFileInCSharp.WriteAllBytesFile(memoryStream, Path.Combine([destinationPath, "WriteAllBytes.png"])); + var path = Path.Combine(FilesPath, "WriteByte.png"); + using var memoryStream = GetMemoryStream(); + + ConvertAStreamToAFileInCSharp.WriteByteToFileStream(memoryStream, path); - Assert.IsTrue(File.Exists(Path.Combine([destinationPath, "WriteAllBytes.png"]))); + CollectionAssert.AreEqual(File.ReadAllBytes(SourceFile), File.ReadAllBytes(path)); } - private string GetPath() + [TestMethod] + public void WhenWriteAllBytesToFile_ThenFileHasTheSourceBytes() { - var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - if (!Directory.Exists(directory)) - { - return string.Empty; - } + var path = Path.Combine(FilesPath, "WriteAllBytes.png"); + using var memoryStream = GetMemoryStream(); - var destinationPath = Path.Combine([directory, "Files"]); + ConvertAStreamToAFileInCSharp.WriteAllBytesFile(memoryStream, path); - return destinationPath; + CollectionAssert.AreEqual(File.ReadAllBytes(SourceFile), File.ReadAllBytes(path)); } - private MemoryStream GetMemoryStream() + [TestMethod] + public void GivenAStreamAtItsEnd_WhenCopyToWithoutRewinding_ThenFileIsEmpty() { - var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - if (!Directory.Exists(directory)) + var path = Path.Combine(FilesPath, "NoRewind.png"); + using var memoryStream = GetMemoryStream(); + Assert.AreEqual(memoryStream.Length, memoryStream.Position); + + using (var fileStream = File.Create(path)) { - return null!; + memoryStream.CopyTo(fileStream); } - var sourceFile = Path.Combine([directory, "Files", "source.png"]); + Assert.AreEqual(0, new FileInfo(path).Length); + } + + [TestMethod] + public void WhenCopyTo_ThenBothPositionsEndAfterTheCopiedBytes() + { + using var memoryStream = GetMemoryStream(); + memoryStream.Position = 0; + using var destination = new MemoryStream(); - using var fileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read); + memoryStream.CopyTo(destination); + + Assert.AreEqual(memoryStream.Length, memoryStream.Position); + Assert.AreEqual(memoryStream.Length, destination.Position); + } + + private static MemoryStream GetMemoryStream() + { + using var fileStream = new FileStream(SourceFile, FileMode.Open, FileAccess.Read); var memoryStream = new MemoryStream(); fileStream.CopyTo(memoryStream); return memoryStream; } -} \ No newline at end of file +} diff --git a/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/Tests/Tests.csproj b/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/Tests/Tests.csproj index df3ab6c81f..2425d93bd4 100644 --- a/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/Tests/Tests.csproj +++ b/csharp-intermediate-topics/HowToConvertAStreamToAFileInCSharp/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net8.0 + net11.0 enable enable @@ -10,10 +10,10 @@ - - - - + + + +