using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
namespace Jellyfin.Extensions
{
///
/// Class BaseExtensions.
///
public static class StreamExtensions
{
///
/// Reads all lines in the .
///
/// The to read from.
/// All lines in the stream.
public static string[] ReadAllLines(this Stream stream)
=> ReadAllLines(stream, Encoding.UTF8);
///
/// Reads all lines in the .
///
/// The to read from.
/// The character encoding to use.
/// All lines in the stream.
public static string[] ReadAllLines(this Stream stream, Encoding encoding)
{
using StreamReader reader = new StreamReader(stream, encoding);
return ReadAllLines(reader).ToArray();
}
///
/// Reads all lines in the .
///
/// The to read from.
/// All lines in the stream.
public static IEnumerable ReadAllLines(this TextReader reader)
{
string? line;
while ((line = reader.ReadLine()) is not null)
{
yield return line;
}
}
///
/// Reads all lines in the .
///
/// The to read from.
/// The token to monitor for cancellation requests.
/// All lines in the stream.
public static async IAsyncEnumerable ReadAllLinesAsync(this TextReader reader, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
string? line;
while ((line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false)) is not null)
{
yield return line;
}
}
}
}