From ac19885f50cc7c899a734352d4ff242dd316701a Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Tue, 15 Sep 2026 15:44:46 +0200 Subject: [PATCH] Convert a stream to a byte array: retarget net10.0, add ReadExactly, CopyTo and the reverse direction Retargets both projects from net7.0 to net10.0 and moves the packages to current stable versions (BenchmarkDotNet 0.15.8, Microsoft.NET.Test.Sdk 18.10.1, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1). Adds UseReadExactly() and UseCopyTo() to ConvertStreamToByteArray, which are the two calls the article now recommends, and UseMemoryStreamConstructor() and UseWritableMemoryStream() for the reverse direction. Four xunit tests cover them, and UseReadExactly and UseCopyTo also join ConvertMethodsBenchmark so the published benchmark measures the recommended methods. Build is clean and tests run 9 of 9 on SDK 10.0.302. --- .../ConvertMethodsBenchmark.cs | 14 +++++++ .../ConvertStreamToByteArray.cs | 31 ++++++++++++++ .../HowToConvertAStreamToAByteArray.csproj | 4 +- .../Tests/ConvertMethodsTest.cs | 42 +++++++++++++++++++ .../Tests/Tests.csproj | 10 ++--- 5 files changed, 94 insertions(+), 7 deletions(-) diff --git a/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray/ConvertMethodsBenchmark.cs b/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray/ConvertMethodsBenchmark.cs index 6fbb633327..dfe99254f4 100644 --- a/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray/ConvertMethodsBenchmark.cs +++ b/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray/ConvertMethodsBenchmark.cs @@ -18,6 +18,20 @@ public void Setup() _convertStreamToByteArray = new ConvertStreamToByteArray(); } + [Benchmark] + public void UseReadExactly() + { + using var _benchmarkStream = new FileStream(_sampleFilePath, FileMode.Open, FileAccess.Read); + _convertStreamToByteArray.UseReadExactly(_benchmarkStream); + } + + [Benchmark] + public void UseCopyTo() + { + using var _benchmarkStream = new FileStream(_sampleFilePath, FileMode.Open, FileAccess.Read); + _convertStreamToByteArray.UseCopyTo(_benchmarkStream); + } + [Benchmark] public void UseStreamDotReadMethod() { diff --git a/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray/ConvertStreamToByteArray.cs b/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray/ConvertStreamToByteArray.cs index 8afd81513d..543b7cb338 100644 --- a/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray/ConvertStreamToByteArray.cs +++ b/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray/ConvertStreamToByteArray.cs @@ -2,6 +2,23 @@ { public class ConvertStreamToByteArray { + public byte[] UseReadExactly(Stream stream) + { + var bytes = new byte[stream.Length]; + stream.ReadExactly(bytes); + + return bytes; + } + + public byte[] UseCopyTo(Stream stream) + { + using var memoryStream = new MemoryStream(); + stream.CopyTo(memoryStream); + + return memoryStream.ToArray(); + } + + public byte[] UseStreamDotReadMethod(Stream stream) { byte[] bytes; @@ -73,5 +90,19 @@ public byte[] UseBufferedStream(Stream stream) return bytes; } + + public Stream UseMemoryStreamConstructor(byte[] bytes) + { + return new MemoryStream(bytes); + } + + public Stream UseWritableMemoryStream(byte[] bytes) + { + var stream = new MemoryStream(); + stream.Write(bytes, 0, bytes.Length); + stream.Position = 0; + + return stream; + } } } \ No newline at end of file diff --git a/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray.csproj b/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray.csproj index 98bdba87fa..c87bc06872 100644 --- a/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray.csproj +++ b/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray/HowToConvertAStreamToAByteArray.csproj @@ -2,13 +2,13 @@ Exe - net7.0 + net10.0 enable disable - + diff --git a/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/Tests/ConvertMethodsTest.cs b/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/Tests/ConvertMethodsTest.cs index 5c8e7bfde8..a8f4eb21e0 100644 --- a/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/Tests/ConvertMethodsTest.cs +++ b/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/Tests/ConvertMethodsTest.cs @@ -57,5 +57,47 @@ public void GivenAStream_WhenConvertingWithStreamReader_ThenReturnsAByteArray() Assert.Equal(_expected, result); } + + [Fact] + public void GivenAStream_WhenConvertingWithReadExactly_ThenReturnsAByteArray() + { + var result = _converter.UseReadExactly(_stream); + + Assert.Equal(_expected, result); + } + + [Fact] + public void GivenAStream_WhenConvertingWithCopyTo_ThenReturnsAByteArray() + { + var result = _converter.UseCopyTo(_stream); + + Assert.Equal(_expected, result); + } + + [Fact] + public void GivenAByteArray_WhenWrappingInAMemoryStreamConstructor_ThenStreamReadsBackTheSameBytes() + { + var result = _converter.UseMemoryStreamConstructor(_expected); + + Assert.Equal(_expected.Length, result.Length); + Assert.Equal(_expected, ReadToEnd(result)); + } + + [Fact] + public void GivenAByteArray_WhenWritingToAnExpandableMemoryStream_ThenStreamReadsBackTheSameBytes() + { + var result = _converter.UseWritableMemoryStream(_expected); + + Assert.Equal(_expected.Length, result.Length); + Assert.Equal(_expected, ReadToEnd(result)); + } + + private static byte[] ReadToEnd(Stream stream) + { + using var memoryStream = new MemoryStream(); + stream.CopyTo(memoryStream); + + return memoryStream.ToArray(); + } } } \ No newline at end of file diff --git a/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/Tests/Tests.csproj b/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/Tests/Tests.csproj index 7c92e8e469..521bd5dfa1 100644 --- a/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/Tests/Tests.csproj +++ b/csharp-intermediate-topics/HowToConvertAStreamToAByteArray/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable disable @@ -9,13 +9,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all