StreamExtensions.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. {
  15. public static class StreamExtensions
  16. {
  17. /// <summary>
  18. /// 将流转换为内存流
  19. /// </summary>
  20. /// <param name="stream"></param>
  21. /// <returns></returns>
  22. public static PooledMemoryStream SaveAsMemoryStream(this Stream stream)
  23. {
  24. stream.Seek(0, SeekOrigin.Begin);
  25. var ms = new PooledMemoryStream();
  26. stream.CopyTo(ms);
  27. return ms;
  28. }
  29. /// <summary>
  30. ///
  31. /// </summary>
  32. /// <param name="stream"></param>
  33. /// <returns></returns>
  34. public static byte[] ToArray(this Stream stream)
  35. {
  36. stream.Position = 0;
  37. byte[] bytes = new byte[stream.Length];
  38. stream.Read(bytes, 0, bytes.Length);
  39. // 设置当前流的位置为流的开始
  40. stream.Seek(0, SeekOrigin.Begin);
  41. return bytes;
  42. }
  43. /// <summary>
  44. /// 流洗码,在流的末端随即增加几个空字节,重要数据请谨慎使用,可能造成流损坏
  45. /// </summary>
  46. /// <param name="stream"></param>
  47. public static void ShuffleCode(this Stream stream)
  48. {
  49. if (stream.CanWrite && stream.CanSeek)
  50. {
  51. var position = stream.Position;
  52. stream.Position = stream.Length;
  53. for (int i = 0; i < new Random().Next(1, 20); i++)
  54. {
  55. stream.WriteByte(0);
  56. }
  57. stream.Flush();
  58. stream.Position = position;
  59. }
  60. }
  61. /// <summary>
  62. /// 读取所有行
  63. /// </summary>
  64. /// <param name="stream"></param>
  65. /// <param name="closeAfter">读取完毕后关闭流</param>
  66. /// <returns></returns>
  67. public static List<string> ReadAllLines(this StreamReader stream, bool closeAfter = true)
  68. {
  69. var stringList = new List<string>();
  70. string str;
  71. while ((str = stream.ReadLine()) != null)
  72. {
  73. stringList.Add(str);
  74. }
  75. if (closeAfter)
  76. {
  77. stream.Close();
  78. stream.Dispose();
  79. }
  80. return stringList;
  81. }
  82. /// <summary>
  83. /// 读取所有行
  84. /// </summary>
  85. /// <param name="stream"></param>
  86. /// <param name="encoding"></param>
  87. /// <param name="closeAfter">读取完毕后关闭流</param>
  88. /// <returns></returns>
  89. public static List<string> ReadAllLines(this FileStream stream, Encoding encoding, bool closeAfter = true)
  90. {
  91. var stringList = new List<string>();
  92. string str;
  93. var sr = new StreamReader(stream, encoding);
  94. while ((str = sr.ReadLine()) != null)
  95. {
  96. stringList.Add(str);
  97. }
  98. if (closeAfter)
  99. {
  100. sr.Close();
  101. sr.Dispose();
  102. stream.Close();
  103. stream.Dispose();
  104. }
  105. return stringList;
  106. }
  107. /// <summary>
  108. /// 读取所有文本
  109. /// </summary>
  110. /// <param name="stream"></param>
  111. /// <param name="encoding"></param>
  112. /// <param name="closeAfter">读取完毕后关闭流</param>
  113. /// <returns></returns>
  114. public static string ReadAllText(this FileStream stream, Encoding encoding, bool closeAfter = true)
  115. {
  116. var sr = new StreamReader(stream, encoding);
  117. var text = sr.ReadToEnd();
  118. if (closeAfter)
  119. {
  120. sr.Close();
  121. sr.Dispose();
  122. stream.Close();
  123. stream.Dispose();
  124. }
  125. return text;
  126. }
  127. /// <summary>
  128. /// 写入所有文本
  129. /// </summary>
  130. /// <param name="stream"></param>
  131. /// <param name="content"></param>
  132. /// <param name="encoding"></param>
  133. /// <param name="closeAfter">读取完毕后关闭流</param>
  134. /// <returns></returns>
  135. public static void WriteAllText(this FileStream stream, string content, Encoding encoding, bool closeAfter = true)
  136. {
  137. var sw = new StreamWriter(stream, encoding);
  138. stream.SetLength(0);
  139. sw.Write(content);
  140. if (closeAfter)
  141. {
  142. sw.Close();
  143. sw.Dispose();
  144. stream.Close();
  145. stream.Dispose();
  146. }
  147. }
  148. /// <summary>
  149. /// 写入所有文本行
  150. /// </summary>
  151. /// <param name="stream"></param>
  152. /// <param name="lines"></param>
  153. /// <param name="encoding"></param>
  154. /// <param name="closeAfter">读取完毕后关闭流</param>
  155. /// <returns></returns>
  156. public static void WriteAllLines(this FileStream stream, IEnumerable<string> lines, Encoding encoding, bool closeAfter = true)
  157. {
  158. var sw = new StreamWriter(stream, encoding);
  159. stream.SetLength(0);
  160. foreach (var line in lines)
  161. {
  162. sw.WriteLine(line);
  163. }
  164. sw.Flush();
  165. if (closeAfter)
  166. {
  167. sw.Close();
  168. sw.Dispose();
  169. stream.Close();
  170. stream.Dispose();
  171. }
  172. }
  173. /// <summary>
  174. /// 共享读写打开文件
  175. /// </summary>
  176. /// <param name="file"></param>
  177. /// <returns></returns>
  178. public static FileStream ShareReadWrite(this FileInfo file)
  179. {
  180. return file.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
  181. }
  182. /// <summary>
  183. /// 读取所有行
  184. /// </summary>
  185. /// <param name="stream"></param>
  186. /// <param name="closeAfter">读取完毕后关闭流</param>
  187. /// <returns></returns>
  188. public static async Task<List<string>> ReadAllLinesAsync(this StreamReader stream, bool closeAfter = true)
  189. {
  190. var stringList = new List<string>();
  191. string str;
  192. while ((str = await stream.ReadLineAsync().ConfigureAwait(false)) != null)
  193. {
  194. stringList.Add(str);
  195. }
  196. if (closeAfter)
  197. {
  198. stream.Close();
  199. stream.Dispose();
  200. }
  201. return stringList;
  202. }
  203. /// <summary>
  204. /// 读取所有行
  205. /// </summary>
  206. /// <param name="stream"></param>
  207. /// <param name="encoding"></param>
  208. /// <param name="closeAfter">读取完毕后关闭流</param>
  209. /// <returns></returns>
  210. public static async Task<List<string>> ReadAllLinesAsync(this FileStream stream, Encoding encoding, bool closeAfter = true)
  211. {
  212. var stringList = new List<string>();
  213. string str;
  214. var sr = new StreamReader(stream, encoding);
  215. while ((str = await sr.ReadLineAsync().ConfigureAwait(false)) != null)
  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. }
  330. }