astral-api/Astral.Core/Extensions/StreamExtensions.cs
Mike 81aa0ec1c0
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful
WIP heartbeat and user presence
2024-12-15 16:06:14 +00:00

29 lines
919 B
C#

// <copyright file="StreamExtensions.cs" company="alveus.dev">
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
// </copyright>
namespace Astral.Core.Extensions;
/// <summary>
/// <see cref="Stream"/> extensions.
/// </summary>
public static class StreamExtensions
{
/// <summary>
/// Return a byte array of the contents of the stream.
/// </summary>
/// <param name="stream">Instance of <see cref="Stream"/>.</param>
/// <returns>Collection of bytes.</returns>
public static byte[] ToByteArray(this Stream stream)
{
stream.Position = 0;
var buffer = new byte[stream.Length];
for (var totalBytesCopied = 0; totalBytesCopied < stream.Length;)
{
totalBytesCopied +=
stream.Read(buffer, totalBytesCopied, Convert.ToInt32(stream.Length) - totalBytesCopied);
}
return buffer;
}
}