StreamExtensions.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. stream.SetLength(0);
  117. sw.Write(content);
  118. if (closeAfter)
  119. {
  120. sw.Close();
  121. sw.Dispose();
  122. stream.Close();
  123. stream.Dispose();
  124. }
  125. }
  126. /// <summary>
  127. /// 写入所有文本行
  128. /// </summary>
  129. /// <param name="stream"></param>
  130. /// <param name="lines"></param>
  131. /// <param name="encoding"></param>
  132. /// <param name="closeAfter">读取完毕后关闭流</param>
  133. /// <returns></returns>
  134. public static void WriteAllLines(this FileStream stream, IEnumerable<string> lines, Encoding encoding, bool closeAfter = true)
  135. {
  136. var sw = new StreamWriter(stream, encoding);
  137. stream.SetLength(0);
  138. foreach (var line in lines)
  139. {
  140. sw.WriteLine(line);
  141. }
  142. sw.Flush();
  143. if (closeAfter)
  144. {
  145. sw.Close();
  146. sw.Dispose();
  147. stream.Close();
  148. stream.Dispose();
  149. }
  150. }
  151. /// <summary>
  152. /// 共享读写打开文件
  153. /// </summary>
  154. /// <param name="file"></param>
  155. /// <returns></returns>
  156. public static FileStream ShareReadWrite(this FileInfo file)
  157. {
  158. return file.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
  159. }
  160. /// <summary>
  161. /// 读取所有行
  162. /// </summary>
  163. /// <param name="stream"></param>
  164. /// <param name="closeAfter">读取完毕后关闭流</param>
  165. /// <returns></returns>
  166. public static async Task<List<string>> ReadAllLinesAsync(this StreamReader stream, bool closeAfter = true)
  167. {
  168. var stringList = new List<string>();
  169. string str;
  170. while ((str = await stream.ReadLineAsync().ConfigureAwait(false)) != null)
  171. {
  172. stringList.Add(str);
  173. }
  174. if (closeAfter)
  175. {
  176. stream.Close();
  177. stream.Dispose();
  178. }
  179. return stringList;
  180. }
  181. /// <summary>
  182. /// 读取所有行
  183. /// </summary>
  184. /// <param name="stream"></param>
  185. /// <param name="encoding"></param>
  186. /// <param name="closeAfter">读取完毕后关闭流</param>
  187. /// <returns></returns>
  188. public static async Task<List<string>> ReadAllLinesAsync(this FileStream stream, Encoding encoding, bool closeAfter = true)
  189. {
  190. var stringList = new List<string>();
  191. string str;
  192. var sr = new StreamReader(stream, encoding);
  193. while ((str = await sr.ReadLineAsync().ConfigureAwait(false)) != null)
  194. {
  195. stringList.Add(str);
  196. }
  197. if (closeAfter)
  198. {
  199. sr.Close();
  200. sr.Dispose();
  201. stream.Close();
  202. #if NET5_0_OR_GREATER
  203. await stream.DisposeAsync().ConfigureAwait(false);
  204. #else
  205. stream.Dispose();
  206. #endif
  207. }
  208. return stringList;
  209. }
  210. /// <summary>
  211. /// 读取所有文本
  212. /// </summary>
  213. /// <param name="stream"></param>
  214. /// <param name="encoding"></param>
  215. /// <param name="closeAfter">读取完毕后关闭流</param>
  216. /// <returns></returns>
  217. public static async Task<string> ReadAllTextAsync(this FileStream stream, Encoding encoding, bool closeAfter = true)
  218. {
  219. var sr = new StreamReader(stream, encoding);
  220. var text = await sr.ReadToEndAsync().ConfigureAwait(false);
  221. if (closeAfter)
  222. {
  223. sr.Close();
  224. sr.Dispose();
  225. stream.Close();
  226. #if NET5_0_OR_GREATER
  227. await stream.DisposeAsync().ConfigureAwait(false);
  228. #else
  229. stream.Dispose();
  230. #endif
  231. }
  232. return text;
  233. }
  234. /// <summary>
  235. /// 写入所有文本
  236. /// </summary>
  237. /// <param name="stream"></param>
  238. /// <param name="content"></param>
  239. /// <param name="encoding"></param>
  240. /// <param name="closeAfter">读取完毕后关闭流</param>
  241. /// <returns></returns>
  242. public static async Task WriteAllTextAsync(this FileStream stream, string content, Encoding encoding, bool closeAfter = true)
  243. {
  244. var sw = new StreamWriter(stream, encoding);
  245. stream.SetLength(0);
  246. await sw.WriteAsync(content).ConfigureAwait(false);
  247. await sw.FlushAsync().ConfigureAwait(false);
  248. if (closeAfter)
  249. {
  250. sw.Close();
  251. stream.Close();
  252. #if NET5_0_OR_GREATER
  253. await sw.DisposeAsync().ConfigureAwait(false);
  254. await stream.DisposeAsync().ConfigureAwait(false);
  255. #else
  256. sw.Dispose();
  257. stream.Dispose();
  258. #endif
  259. }
  260. }
  261. /// <summary>
  262. /// 写入所有文本行
  263. /// </summary>
  264. /// <param name="stream"></param>
  265. /// <param name="lines"></param>
  266. /// <param name="encoding"></param>
  267. /// <param name="closeAfter">读取完毕后关闭流</param>
  268. /// <returns></returns>
  269. public static async Task WriteAllLinesAsync(this FileStream stream, IEnumerable<string> lines, Encoding encoding, bool closeAfter = true)
  270. {
  271. var sw = new StreamWriter(stream, encoding);
  272. stream.SetLength(0);
  273. foreach (var line in lines)
  274. {
  275. await sw.WriteLineAsync(line).ConfigureAwait(false);
  276. }
  277. await sw.FlushAsync().ConfigureAwait(false);
  278. if (closeAfter)
  279. {
  280. sw.Close();
  281. stream.Close();
  282. #if NET5_0_OR_GREATER
  283. await sw.DisposeAsync().ConfigureAwait(false);
  284. await stream.DisposeAsync().ConfigureAwait(false);
  285. #else
  286. sw.Dispose();
  287. stream.Dispose();
  288. #endif
  289. }
  290. }
  291. #if NET5_0_OR_GREATER
  292. /// <summary>
  293. ///
  294. /// </summary>
  295. /// <param name="stream"></param>
  296. /// <param name="cancellationToken"></param>
  297. /// <returns></returns>
  298. public static async Task<byte[]> ToArrayAsync(this Stream stream, CancellationToken cancellationToken = default)
  299. {
  300. stream.Position = 0;
  301. byte[] bytes = new byte[stream.Length];
  302. await stream.ReadAsync(bytes, cancellationToken);
  303. stream.Seek(0, SeekOrigin.Begin);
  304. return bytes;
  305. }
  306. #endif
  307. }
  308. }