| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | using System.Text; |
| | | 6 | | using System.Threading; |
| | | 7 | | |
| | | 8 | | namespace Jellyfin.Extensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Class BaseExtensions. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class StreamExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Reads all lines in the <see cref="Stream" />. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="stream">The <see cref="Stream" /> to read from.</param> |
| | | 19 | | /// <returns>All lines in the stream.</returns> |
| | | 20 | | public static string[] ReadAllLines(this Stream stream) |
| | 5 | 21 | | => ReadAllLines(stream, Encoding.UTF8); |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Reads all lines in the <see cref="Stream" />. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="stream">The <see cref="Stream" /> to read from.</param> |
| | | 27 | | /// <param name="encoding">The character encoding to use.</param> |
| | | 28 | | /// <returns>All lines in the stream.</returns> |
| | | 29 | | public static string[] ReadAllLines(this Stream stream, Encoding encoding) |
| | | 30 | | { |
| | 5 | 31 | | using StreamReader reader = new StreamReader(stream, encoding); |
| | 5 | 32 | | return ReadAllLines(reader).ToArray(); |
| | 5 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Reads all lines in the <see cref="TextReader" />. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="reader">The <see cref="TextReader" /> to read from.</param> |
| | | 39 | | /// <returns>All lines in the stream.</returns> |
| | | 40 | | public static IEnumerable<string> ReadAllLines(this TextReader reader) |
| | | 41 | | { |
| | | 42 | | string? line; |
| | | 43 | | while ((line = reader.ReadLine()) is not null) |
| | | 44 | | { |
| | | 45 | | yield return line; |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Reads all lines in the <see cref="TextReader" />. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="reader">The <see cref="TextReader" /> to read from.</param> |
| | | 53 | | /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> |
| | | 54 | | /// <returns>All lines in the stream.</returns> |
| | | 55 | | public static async IAsyncEnumerable<string> ReadAllLinesAsync(this TextReader reader, [EnumeratorCancellation] |
| | | 56 | | { |
| | | 57 | | string? line; |
| | | 58 | | while ((line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false)) is not null) |
| | | 59 | | { |
| | | 60 | | yield return line; |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | } |