StreamExtensions.cs 10 KB

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