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