using System; using System.Buffers; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Jellyfin.Extensions { /// /// Class BaseExtensions. /// public static class StreamExtensions { private const int StreamComparisonBufferSize = 65536; /// /// 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; } } /// /// Determines whether a stream is identical to a file on disk. /// /// The stream to compare. /// The file path to compare against. /// The token to monitor for cancellation requests. /// True if the stream and file are identical; otherwise false. public static async Task IsFileIdenticalAsync(this Stream stream, string path, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(stream); ArgumentException.ThrowIfNullOrEmpty(path); if (!stream.CanSeek) { return false; } var originalPosition = stream.Position; try { stream.Position = 0; var existingFileStream = new FileStream( path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: StreamComparisonBufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan); await using (existingFileStream.ConfigureAwait(false)) { return await stream.IsStreamIdenticalAsync(existingFileStream, cancellationToken).ConfigureAwait(false); } } finally { stream.Position = originalPosition; } } /// /// Determines whether two streams are identical. /// /// The first stream to compare. /// The second stream to compare. /// The token to monitor for cancellation requests. /// True if the streams are identical; otherwise false. public static async Task IsStreamIdenticalAsync(this Stream a, Stream b, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(a); ArgumentNullException.ThrowIfNull(b); if (b.Length != a.Length) { return false; } var bufferA = ArrayPool.Shared.Rent(StreamComparisonBufferSize); var bufferB = ArrayPool.Shared.Rent(StreamComparisonBufferSize); try { while (true) { cancellationToken.ThrowIfCancellationRequested(); var bytesReadA = await a.ReadAsync(bufferA.AsMemory(), cancellationToken).ConfigureAwait(false); var bytesReadB = await b.ReadAsync(bufferB.AsMemory(), cancellationToken).ConfigureAwait(false); if (bytesReadA != bytesReadB) { return false; } if (bytesReadA == 0) { return true; } if (!bufferA.AsSpan(0, bytesReadA).SequenceEqual(bufferB.AsSpan(0, bytesReadB))) { return false; } } } finally { ArrayPool.Shared.Return(bufferA); ArrayPool.Shared.Return(bufferB); } } } }