Skip to content

Commit

Permalink
feat: Added async extensions to IFileInfo (#66)
Browse files Browse the repository at this point in the history
* Added all async calls from IFile
* Added EnumerateLinesAsync
  • Loading branch information
gigi81 authored Mar 1, 2024
1 parent ad6ba9b commit 55aaeeb
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
98 changes: 98 additions & 0 deletions src/System.IO.Abstractions.Extensions/IFileInfoAsyncExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
#nullable enable

using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace System.IO.Abstractions
{
public static class FileInfoFileAsyncExtensions
{
/// <inheritdoc cref="IFile.AppendAllLinesAsync(string,System.Collections.Generic.IEnumerable{string},System.Threading.CancellationToken)"/>
public static async Task AppendAllLinesAsync(this IFileInfo file, IEnumerable<string> contents, CancellationToken cancellationToken = default)
{
await file.FileSystem.File.AppendAllLinesAsync(file.FullName, contents, cancellationToken);
}

/// <inheritdoc cref="IFile.AppendAllTextAsync(string,string?,System.Threading.CancellationToken)"/>
public static async Task AppendAllTextAsync(this IFileInfo file, string? contents, CancellationToken cancellationToken = default)
{
await file.FileSystem.File.AppendAllTextAsync(file.FullName, contents, cancellationToken);
}

/// <inheritdoc cref="IFile.ReadAllBytesAsync(string,System.Threading.CancellationToken)"/>
public static async Task<byte[]> ReadAllBytesAsync(this IFileInfo file, CancellationToken cancellationToken = default)
{
return await file.FileSystem.File.ReadAllBytesAsync(file.FullName, cancellationToken);
}

/// <inheritdoc cref="IFile.ReadAllLinesAsync(string,System.Threading.CancellationToken)"/>
public static async Task<string[]> ReadAllLinesAsync(this IFileInfo file, CancellationToken cancellationToken = default)
{
return await file.FileSystem.File.ReadAllLinesAsync(file.FullName, cancellationToken);
}

/// <inheritdoc cref="IFile.ReadAllTextAsync(string,System.Threading.CancellationToken)" />
public static async Task<string> ReadAllTextAsync(this IFileInfo file, CancellationToken cancellationToken = default)
{
return await file.FileSystem.File.ReadAllTextAsync(file.FullName, cancellationToken);
}

/// <inheritdoc cref="IFile.WriteAllBytesAsync(string,byte[],System.Threading.CancellationToken)" />
public static async Task WriteAllBytesAsync(this IFileInfo file, byte[] bytes, CancellationToken cancellationToken = default)
{
await file.FileSystem.File.WriteAllBytesAsync(file.FullName, bytes, cancellationToken);
}

/// <inheritdoc cref="IFile.WriteAllLinesAsync(string,System.Collections.Generic.IEnumerable{string},System.Threading.CancellationToken)" />
public static async Task WriteAllLinesAsync(this IFileInfo file, IEnumerable<string> contents, CancellationToken cancellationToken = default)
{
await file.FileSystem.File.WriteAllLinesAsync(file.FullName, contents, cancellationToken);
}

/// <inheritdoc cref="IFile.WriteAllTextAsync(string,string?,System.Threading.CancellationToken)" />
public static async Task WriteAllTextAsync(this IFileInfo file, string? contents, CancellationToken cancellationToken = default)
{
await file.FileSystem.File.WriteAllTextAsync(file.FullName, contents, cancellationToken);
}

/// <summary>
/// Creates an <see cref="IAsyncEnumerable{String}"/> that can enumerate asynchronously the lines of text in the specified <paramref name="file"/>
/// </summary>
/// <param name="file">File to enumerate content</param>
/// <param name="cancellationToken"></param>
/// <returns>Returns an <see cref="IAsyncEnumerable{String}"/> to enumerate the content of the file</returns>
public static IAsyncEnumerable<string> EnumerateLinesAsync(this IFileInfo file, CancellationToken cancellationToken)
{
return EnumerateLinesAsync(file, null, cancellationToken);
}

/// <summary>
/// Creates an <see cref="IAsyncEnumerable{String}"/> that can enumerate asynchronously the lines of text in the specified <paramref name="file"/>
/// using the specified <paramref name="encoding"/>
/// </summary>
/// <param name="file">File to enumerate content</param>
/// <param name="encoding">Encoding to use to read the file</param>
/// <param name="cancellationToken"></param>
/// <returns>Returns an <see cref="IAsyncEnumerable{String}"/> to enumerate the content of the file</returns>
public static async IAsyncEnumerable<string> EnumerateLinesAsync(this IFileInfo file, Encoding? encoding, [EnumeratorCancellation] CancellationToken cancellationToken)
{
await using var stream = file.OpenRead();
using var reader = encoding == null
? new StreamReader(stream)
: new StreamReader(stream, encoding);

var line = await reader.ReadLineAsync();
while (line != null)
{
yield return line;
cancellationToken.ThrowIfCancellationRequested();
line = await reader.ReadLineAsync();
}
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static IEnumerable<string> EnumerateLines(this IFileInfo file)
/// using the specified <paramref name="encoding"/>
/// </summary>
/// <param name="file">File to enumerate content</param>
/// <param name="encoding">Encoding to use to read the file</param>
/// <returns>Returns an <see cref="IEnumerable{String}"/> to enumerate the content of the file</returns>
public static IEnumerable<string> EnumerateLines(this IFileInfo file, Encoding encoding)
{
Expand Down

0 comments on commit 55aaeeb

Please sign in to comment.