You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
2.2 KiB
64 lines
2.2 KiB
4 years ago
|
using System.Collections.Generic;
|
||
|
using System.IO;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
|
||
4 years ago
|
namespace Jellyfin.Extensions
|
||
4 years ago
|
{
|
||
|
/// <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>
|
||
4 years ago
|
/// Reads all lines in the <see cref="TextReader" />.
|
||
4 years ago
|
/// </summary>
|
||
4 years ago
|
/// <param name="reader">The <see cref="TextReader" /> to read from.</param>
|
||
4 years ago
|
/// <returns>All lines in the stream.</returns>
|
||
4 years ago
|
public static IEnumerable<string> ReadAllLines(this TextReader reader)
|
||
4 years ago
|
{
|
||
|
string? line;
|
||
|
while ((line = reader.ReadLine()) != null)
|
||
|
{
|
||
|
yield return line;
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
|
/// <summary>
|
||
|
/// Reads all lines in the <see cref="TextReader" />.
|
||
|
/// </summary>
|
||
|
/// <param name="reader">The <see cref="TextReader" /> to read from.</param>
|
||
|
/// <returns>All lines in the stream.</returns>
|
||
|
public static async IAsyncEnumerable<string> ReadAllLinesAsync(this TextReader reader)
|
||
|
{
|
||
|
string? line;
|
||
|
while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
|
||
|
{
|
||
|
yield return line;
|
||
|
}
|
||
|
}
|
||
4 years ago
|
}
|
||
|
}
|