Skip to content

Commit

Permalink
Merge branch 'master' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuDanK committed Apr 7, 2022
2 parents 6b55dc5 + 9af81fd commit 7b925ae
Show file tree
Hide file tree
Showing 3 changed files with 629 additions and 4 deletions.
35 changes: 35 additions & 0 deletions SageTools/Extension/Stream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace SageTools.Extension
{
public static partial class Extension
{
/// <summary>
/// 将流读为字符串
/// 注:默认使用UTF-8编码
/// </summary>
/// <param name="stream">流</param>
/// <param name="encoding">指定编码</param>
/// <returns></returns>
public static async Task<string> ReadToStringAsync(this Stream stream, Encoding encoding = null)
{
encoding ??= Encoding.UTF8;

if (stream.CanSeek)
{
stream.Seek(0, SeekOrigin.Begin);
}

var resStr = await new StreamReader(stream, encoding).ReadToEndAsync();

if (stream.CanSeek)
{
stream.Seek(0, SeekOrigin.Begin);
}

return resStr;
}
}
}
Loading

0 comments on commit 7b925ae

Please sign in to comment.