using System.IO; using System.Threading; using System.Threading.Tasks; namespace Ips.Library.Basic { public static class StreamExtensions { public static byte[] GetAllBytes(this Stream stream) { using (var memoryStream = new MemoryStream()) { if (stream.CanSeek) { stream.Position = 0; } stream.CopyTo(memoryStream); return memoryStream.ToArray(); } } public static async Task GetAllBytesAsync(this Stream stream, CancellationToken cancellationToken = default) { using (var memoryStream = new MemoryStream()) { if (stream.CanSeek) { stream.Position = 0; } await stream.CopyToAsync(memoryStream, cancellationToken); return memoryStream.ToArray(); } } public static Task CopyToAsync(this Stream stream, Stream destination, CancellationToken cancellationToken) { if (stream.CanSeek) { stream.Position = 0; } return stream.CopyToAsync( destination, 81920, //this is already the default value, but needed to set to be able to pass the cancellationToken cancellationToken ); } } }