using System.IO; #if NET5_0 using System; using System.Buffers; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; #endif namespace Masuit.Tools { public static class StreamExtensions { /// /// 将流转换为内存流 /// /// /// public static MemoryStream SaveAsMemoryStream(this Stream stream) { stream.Position = 0; return new MemoryStream(stream.ToArray()); } /// /// /// /// /// public static byte[] ToArray(this Stream stream) { stream.Position = 0; byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); return bytes; } #if NET5_0 /// /// /// /// /// /// public static async Task ToArrayAsync(this Stream stream, CancellationToken cancellationToken = default) { stream.Position = 0; byte[] bytes = new byte[stream.Length]; await stream.ReadAsync(bytes, cancellationToken); stream.Seek(0, SeekOrigin.Begin); return bytes; } #endif } }