From 34ed0d4aed2e384a7961ed0c26328b2e9c007e54 Mon Sep 17 00:00:00 2001 From: "Dominik.Kotecki" Date: Thu, 29 Feb 2024 18:13:09 +0100 Subject: [PATCH] small changes --- src/BuildingBlocks/Common/Common.csproj | 4 +-- .../Common/Extensions/StreamExtensions.cs | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 src/BuildingBlocks/Common/Extensions/StreamExtensions.cs diff --git a/src/BuildingBlocks/Common/Common.csproj b/src/BuildingBlocks/Common/Common.csproj index 61718a1..6b512ec 100644 --- a/src/BuildingBlocks/Common/Common.csproj +++ b/src/BuildingBlocks/Common/Common.csproj @@ -1,3 +1 @@ - - - + diff --git a/src/BuildingBlocks/Common/Extensions/StreamExtensions.cs b/src/BuildingBlocks/Common/Extensions/StreamExtensions.cs new file mode 100644 index 0000000..82ae409 --- /dev/null +++ b/src/BuildingBlocks/Common/Extensions/StreamExtensions.cs @@ -0,0 +1,28 @@ +namespace Common.Extensions; + +public static class StreamExtensions +{ + public static byte[] ReadBytes(this Stream stream, int count) + { + ArgumentOutOfRangeException.ThrowIfNegative(count); + ArgumentNullException.ThrowIfNull(stream); + if (stream.Length == 0) + { + return []; + } + + if (count == 0) + { + return []; + } + + var buffer = new byte[count]; + var readBytes = stream.ReadAtLeast(buffer, count, false); + + if (readBytes < count) + { + Array.Resize(ref buffer, readBytes); + } + return buffer; + } +} \ No newline at end of file