StreamExtensions.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. #if NET5_0_OR_GREATER
  7. using System;
  8. using System.Buffers;
  9. using System.Runtime.InteropServices;
  10. #endif
  11. namespace Masuit.Tools
  12. {
  13. public static class StreamExtensions
  14. {
  15. /// <summary>
  16. /// 将流转换为内存流
  17. /// </summary>
  18. /// <param name="stream"></param>
  19. /// <returns></returns>
  20. public static MemoryStream SaveAsMemoryStream(this Stream stream)
  21. {
  22. stream.Position = 0;
  23. return new MemoryStream(stream.ToArray());
  24. }
  25. /// <summary>
  26. ///
  27. /// </summary>
  28. /// <param name="stream"></param>
  29. /// <returns></returns>
  30. public static byte[] ToArray(this Stream stream)
  31. {
  32. stream.Position = 0;
  33. byte[] bytes = new byte[stream.Length];
  34. stream.Read(bytes, 0, bytes.Length);
  35. // 设置当前流的位置为流的开始
  36. stream.Seek(0, SeekOrigin.Begin);
  37. return bytes;
  38. }
  39. /// <summary>
  40. /// 读取所有行
  41. /// </summary>
  42. /// <param name="stream"></param>
  43. /// <param name="closeAfter">读取完毕后关闭流</param>
  44. /// <returns></returns>
  45. public static List<string> ReadAllLines(this StreamReader stream, bool closeAfter = true)
  46. {
  47. var stringList = new List<string>();
  48. string str;
  49. while ((str = stream.ReadLine()) != null)
  50. {
  51. stringList.Add(str);
  52. }
  53. if (closeAfter)
  54. {
  55. stream.Close();
  56. stream.Dispose();
  57. }
  58. return stringList;
  59. }
  60. /// <summary>
  61. /// 读取所有行
  62. /// </summary>
  63. /// <param name="stream"></param>
  64. /// <param name="encoding"></param>
  65. /// <param name="closeAfter">读取完毕后关闭流</param>
  66. /// <returns></returns>
  67. public static List<string> ReadAllLines(this FileStream stream, Encoding encoding, bool closeAfter = true)
  68. {
  69. var stringList = new List<string>();
  70. string str;
  71. var sr = new StreamReader(stream, encoding);
  72. while ((str = sr.ReadLine()) != null)
  73. {
  74. stringList.Add(str);
  75. }
  76. if (closeAfter)
  77. {
  78. sr.Close();
  79. sr.Dispose();
  80. stream.Close();
  81. stream.Dispose();
  82. }
  83. return stringList;
  84. }
  85. /// <summary>
  86. /// 读取所有文本
  87. /// </summary>
  88. /// <param name="stream"></param>
  89. /// <param name="encoding"></param>
  90. /// <param name="closeAfter">读取完毕后关闭流</param>
  91. /// <returns></returns>
  92. public static string ReadAllText(this FileStream stream, Encoding encoding, bool closeAfter = true)
  93. {
  94. var sr = new StreamReader(stream, encoding);
  95. var text = sr.ReadToEnd();
  96. if (closeAfter)
  97. {
  98. sr.Close();
  99. sr.Dispose();
  100. stream.Close();
  101. stream.Dispose();
  102. }
  103. return text;
  104. }
  105. /// <summary>
  106. /// 写入所有文本
  107. /// </summary>
  108. /// <param name="stream"></param>
  109. /// <param name="content"></param>
  110. /// <param name="encoding"></param>
  111. /// <param name="closeAfter">读取完毕后关闭流</param>
  112. /// <returns></returns>
  113. public static void WriteAllText(this FileStream stream, string content, Encoding encoding, bool closeAfter = true)
  114. {
  115. var sw = new StreamWriter(stream, encoding);
  116. sw.Write(content);
  117. if (closeAfter)
  118. {
  119. sw.Close();
  120. sw.Dispose();
  121. stream.Close();
  122. stream.Dispose();
  123. }
  124. }
  125. /// <summary>
  126. /// 写入所有文本行
  127. /// </summary>
  128. /// <param name="stream"></param>
  129. /// <param name="lines"></param>
  130. /// <param name="encoding"></param>
  131. /// <param name="closeAfter">读取完毕后关闭流</param>
  132. /// <returns></returns>
  133. public static void WriteAllLines(this FileStream stream, IEnumerable<string> lines, Encoding encoding, bool closeAfter = true)
  134. {
  135. var sw = new StreamWriter(stream, encoding);
  136. foreach (var line in lines)
  137. {
  138. sw.WriteLine(line);
  139. }
  140. sw.Flush();
  141. if (closeAfter)
  142. {
  143. sw.Close();
  144. sw.Dispose();
  145. stream.Close();
  146. stream.Dispose();
  147. }
  148. }
  149. /// <summary>
  150. /// 共享读写打开文件
  151. /// </summary>
  152. /// <param name="file"></param>
  153. /// <returns></returns>
  154. public static FileStream ShareReadWrite(this FileInfo file)
  155. {
  156. return file.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
  157. }
  158. /// <summary>
  159. /// 读取所有行
  160. /// </summary>
  161. /// <param name="stream"></param>
  162. /// <param name="closeAfter">读取完毕后关闭流</param>
  163. /// <returns></returns>
  164. public static async Task<List<string>> ReadAllLinesAsync(this StreamReader stream, bool closeAfter = true)
  165. {
  166. var stringList = new List<string>();
  167. string str;
  168. while ((str = await stream.ReadLineAsync().ConfigureAwait(false)) != null)
  169. {
  170. stringList.Add(str);
  171. }
  172. if (closeAfter)
  173. {
  174. stream.Close();
  175. stream.Dispose();
  176. }
  177. return stringList;
  178. }
  179. /// <summary>
  180. /// 读取所有行
  181. /// </summary>
  182. /// <param name="stream"></param>
  183. /// <param name="encoding"></param>
  184. /// <param name="closeAfter">读取完毕后关闭流</param>
  185. /// <returns></returns>
  186. public static async Task<List<string>> ReadAllLinesAsync(this FileStream stream, Encoding encoding, bool closeAfter = true)
  187. {
  188. var stringList = new List<string>();
  189. string str;
  190. var sr = new StreamReader(stream, encoding);
  191. while ((str = await sr.ReadLineAsync().ConfigureAwait(false)) != null)
  192. {
  193. stringList.Add(str);
  194. }
  195. if (closeAfter)
  196. {
  197. sr.Close();
  198. sr.Dispose();
  199. stream.Close();
  200. #if NET5_0_OR_GREATER
  201. await stream.DisposeAsync().ConfigureAwait(false);
  202. #else
  203. stream.Dispose();
  204. #endif
  205. }
  206. return stringList;
  207. }
  208. /// <summary>
  209. /// 读取所有文本
  210. /// </summary>
  211. /// <param name="stream"></param>
  212. /// <param name="encoding"></param>
  213. /// <param name="closeAfter">读取完毕后关闭流</param>
  214. /// <returns></returns>
  215. public static async Task<string> ReadAllTextAsync(this FileStream stream, Encoding encoding, bool closeAfter = true)
  216. {
  217. var sr = new StreamReader(stream, encoding);
  218. var text = await sr.ReadToEndAsync().ConfigureAwait(false);
  219. if (closeAfter)
  220. {
  221. sr.Close();
  222. sr.Dispose();
  223. stream.Close();
  224. #if NET5_0_OR_GREATER
  225. await stream.DisposeAsync().ConfigureAwait(false);
  226. #else
  227. stream.Dispose();
  228. #endif
  229. }
  230. return text;
  231. }
  232. /// <summary>
  233. /// 写入所有文本
  234. /// </summary>
  235. /// <param name="stream"></param>
  236. /// <param name="content"></param>
  237. /// <param name="encoding"></param>
  238. /// <param name="closeAfter">读取完毕后关闭流</param>
  239. /// <returns></returns>
  240. public static async Task WriteAllTextAsync(this FileStream stream, string content, Encoding encoding, bool closeAfter = true)
  241. {
  242. var sw = new StreamWriter(stream, encoding);
  243. await sw.WriteAsync(content).ConfigureAwait(false);
  244. await sw.FlushAsync().ConfigureAwait(false);
  245. if (closeAfter)
  246. {
  247. sw.Close();
  248. stream.Close();
  249. #if NET5_0_OR_GREATER
  250. await sw.DisposeAsync().ConfigureAwait(false);
  251. await stream.DisposeAsync().ConfigureAwait(false);
  252. #else
  253. sw.Dispose();
  254. stream.Dispose();
  255. #endif
  256. }
  257. }
  258. /// <summary>
  259. /// 写入所有文本行
  260. /// </summary>
  261. /// <param name="stream"></param>
  262. /// <param name="lines"></param>
  263. /// <param name="encoding"></param>
  264. /// <param name="closeAfter">读取完毕后关闭流</param>
  265. /// <returns></returns>
  266. public static async Task WriteAllLinesAsync(this FileStream stream, IEnumerable<string> lines, Encoding encoding, bool closeAfter = true)
  267. {
  268. var sw = new StreamWriter(stream, encoding);
  269. foreach (var line in lines)
  270. {
  271. await sw.WriteLineAsync(line).ConfigureAwait(false);
  272. }
  273. await sw.FlushAsync().ConfigureAwait(false);
  274. if (closeAfter)
  275. {
  276. sw.Close();
  277. stream.Close();
  278. #if NET5_0_OR_GREATER
  279. await sw.DisposeAsync().ConfigureAwait(false);
  280. await stream.DisposeAsync().ConfigureAwait(false);
  281. #else
  282. sw.Dispose();
  283. stream.Dispose();
  284. #endif
  285. }
  286. }
  287. #if NET5_0_OR_GREATER
  288. /// <summary>
  289. ///
  290. /// </summary>
  291. /// <param name="stream"></param>
  292. /// <param name="cancellationToken"></param>
  293. /// <returns></returns>
  294. public static async Task<byte[]> ToArrayAsync(this Stream stream, CancellationToken cancellationToken = default)
  295. {
  296. stream.Position = 0;
  297. byte[] bytes = new byte[stream.Length];
  298. await stream.ReadAsync(bytes, cancellationToken);
  299. stream.Seek(0, SeekOrigin.Begin);
  300. return bytes;
  301. }
  302. #endif
  303. }
  304. }