123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 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
- {
- /// <summary>
- /// 将流转换为内存流
- /// </summary>
- /// <param name="stream"></param>
- /// <returns></returns>
- public static MemoryStream SaveAsMemoryStream(this Stream stream)
- {
- stream.Position = 0;
- return new MemoryStream(stream.ToArray());
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="stream"></param>
- /// <returns></returns>
- 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
- /// <summary>
- ///
- /// </summary>
- /// <param name="stream"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public static async Task<byte[]> ToArrayAsync(this Stream stream, CancellationToken cancellationToken = default)
- {
- byte[] bytes = new byte[stream.Length];
- await stream.ReadAsync(bytes, cancellationToken);
- stream.Seek(0, SeekOrigin.Begin);// 设置当前流的位置为流的开始
- return bytes;
- }
- #endif
- }
- }
|