aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Extensions/StreamExtensions.cs
diff options
context:
space:
mode:
authorJoshua M. Boniface <joshua@boniface.me>2021-02-22 20:59:49 -0500
committerGitHub <noreply@github.com>2021-02-22 20:59:49 -0500
commit23ff1fab46d13d24203748a4054f055a76aae233 (patch)
tree238ceb41fccf30bf4e19887adbe1fe55b3eb733e /MediaBrowser.Common/Extensions/StreamExtensions.cs
parent6cfec511c317bf51aa9fd8d6f0998c6f8b7af6ef (diff)
parent9e5c4439b9cba6922ccadf880edb9049ce18e824 (diff)
Merge pull request #4984 from Bond-009/subtitleedit
Diffstat (limited to 'MediaBrowser.Common/Extensions/StreamExtensions.cs')
-rw-r--r--MediaBrowser.Common/Extensions/StreamExtensions.cs51
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;
+ }
+ }
+ }
+}