diff options
| author | dkanada <dkanada@users.noreply.github.com> | 2021-02-23 19:19:38 +0900 |
|---|---|---|
| committer | dkanada <dkanada@users.noreply.github.com> | 2021-02-23 19:19:38 +0900 |
| commit | bc746b4d05f440b4910751ab7ae70e81ad470892 (patch) | |
| tree | 0642ed74e91511214e4d4b8a3beb40b9c7a8e7d4 /MediaBrowser.Common/Extensions/StreamExtensions.cs | |
| parent | 9caf3119257544d6fb8fd3e1f1cb2b50eba7bd39 (diff) | |
| parent | 7ece3c552337340a997a75aab1520a501a673f61 (diff) | |
merge branch 'master' into auto-manifest
Diffstat (limited to 'MediaBrowser.Common/Extensions/StreamExtensions.cs')
| -rw-r--r-- | MediaBrowser.Common/Extensions/StreamExtensions.cs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/MediaBrowser.Common/Extensions/StreamExtensions.cs b/MediaBrowser.Common/Extensions/StreamExtensions.cs new file mode 100644 index 000000000..cd77be7b2 --- /dev/null +++ b/MediaBrowser.Common/Extensions/StreamExtensions.cs @@ -0,0 +1,51 @@ +#nullable enable + +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace MediaBrowser.Common.Extensions +{ + /// <summary> + /// Class BaseExtensions. + /// </summary> + public static class StreamExtensions + { + /// <summary> + /// Reads all lines in the <see cref="Stream" />. + /// </summary> + /// <param name="stream">The <see cref="Stream" /> to read from.</param> + /// <returns>All lines in the stream.</returns> + public static string[] ReadAllLines(this Stream stream) + => ReadAllLines(stream, Encoding.UTF8); + + /// <summary> + /// Reads all lines in the <see cref="Stream" />. + /// </summary> + /// <param name="stream">The <see cref="Stream" /> to read from.</param> + /// <param name="encoding">The character encoding to use.</param> + /// <returns>All lines in the stream.</returns> + public static string[] ReadAllLines(this Stream stream, Encoding encoding) + { + using (StreamReader reader = new StreamReader(stream, encoding)) + { + return ReadAllLines(reader).ToArray(); + } + } + + /// <summary> + /// Reads all lines in the <see cref="StreamReader" />. + /// </summary> + /// <param name="reader">The <see cref="StreamReader" /> to read from.</param> + /// <returns>All lines in the stream.</returns> + public static IEnumerable<string> ReadAllLines(this StreamReader reader) + { + string? line; + while ((line = reader.ReadLine()) != null) + { + yield return line; + } + } + } +} |
