StreamExtensions.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #nullable enable
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. namespace MediaBrowser.Common.Extensions
  7. {
  8. /// <summary>
  9. /// Class BaseExtensions.
  10. /// </summary>
  11. public static class StreamExtensions
  12. {
  13. /// <summary>
  14. /// Reads all lines in the <see cref="Stream" />.
  15. /// </summary>
  16. /// <param name="stream">The <see cref="Stream" /> to read from.</param>
  17. /// <returns>All lines in the stream.</returns>
  18. public static string[] ReadAllLines(this Stream stream)
  19. => ReadAllLines(stream, Encoding.UTF8);
  20. /// <summary>
  21. /// Reads all lines in the <see cref="Stream" />.
  22. /// </summary>
  23. /// <param name="stream">The <see cref="Stream" /> to read from.</param>
  24. /// <param name="encoding">The character encoding to use.</param>
  25. /// <returns>All lines in the stream.</returns>
  26. public static string[] ReadAllLines(this Stream stream, Encoding encoding)
  27. {
  28. using (StreamReader reader = new StreamReader(stream, encoding))
  29. {
  30. return ReadAllLines(reader).ToArray();
  31. }
  32. }
  33. /// <summary>
  34. /// Reads all lines in the <see cref="StreamReader" />.
  35. /// </summary>
  36. /// <param name="reader">The <see cref="StreamReader" /> to read from.</param>
  37. /// <returns>All lines in the stream.</returns>
  38. public static IEnumerable<string> ReadAllLines(this StreamReader reader)
  39. {
  40. string? line;
  41. while ((line = reader.ReadLine()) != null)
  42. {
  43. yield return line;
  44. }
  45. }
  46. }
  47. }