29 lines
919 B
C#
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;
|
|
}
|
|
}
|