Skip to content

Commit 55aaeeb

Browse files
authored
feat: Added async extensions to IFileInfo (#66)
* Added all async calls from IFile * Added EnumerateLinesAsync
1 parent ad6ba9b commit 55aaeeb

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
2+
#nullable enable
3+
4+
using System.Collections.Generic;
5+
using System.Runtime.CompilerServices;
6+
using System.Text;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
10+
namespace System.IO.Abstractions
11+
{
12+
public static class FileInfoFileAsyncExtensions
13+
{
14+
/// <inheritdoc cref="IFile.AppendAllLinesAsync(string,System.Collections.Generic.IEnumerable{string},System.Threading.CancellationToken)"/>
15+
public static async Task AppendAllLinesAsync(this IFileInfo file, IEnumerable<string> contents, CancellationToken cancellationToken = default)
16+
{
17+
await file.FileSystem.File.AppendAllLinesAsync(file.FullName, contents, cancellationToken);
18+
}
19+
20+
/// <inheritdoc cref="IFile.AppendAllTextAsync(string,string?,System.Threading.CancellationToken)"/>
21+
public static async Task AppendAllTextAsync(this IFileInfo file, string? contents, CancellationToken cancellationToken = default)
22+
{
23+
await file.FileSystem.File.AppendAllTextAsync(file.FullName, contents, cancellationToken);
24+
}
25+
26+
/// <inheritdoc cref="IFile.ReadAllBytesAsync(string,System.Threading.CancellationToken)"/>
27+
public static async Task<byte[]> ReadAllBytesAsync(this IFileInfo file, CancellationToken cancellationToken = default)
28+
{
29+
return await file.FileSystem.File.ReadAllBytesAsync(file.FullName, cancellationToken);
30+
}
31+
32+
/// <inheritdoc cref="IFile.ReadAllLinesAsync(string,System.Threading.CancellationToken)"/>
33+
public static async Task<string[]> ReadAllLinesAsync(this IFileInfo file, CancellationToken cancellationToken = default)
34+
{
35+
return await file.FileSystem.File.ReadAllLinesAsync(file.FullName, cancellationToken);
36+
}
37+
38+
/// <inheritdoc cref="IFile.ReadAllTextAsync(string,System.Threading.CancellationToken)" />
39+
public static async Task<string> ReadAllTextAsync(this IFileInfo file, CancellationToken cancellationToken = default)
40+
{
41+
return await file.FileSystem.File.ReadAllTextAsync(file.FullName, cancellationToken);
42+
}
43+
44+
/// <inheritdoc cref="IFile.WriteAllBytesAsync(string,byte[],System.Threading.CancellationToken)" />
45+
public static async Task WriteAllBytesAsync(this IFileInfo file, byte[] bytes, CancellationToken cancellationToken = default)
46+
{
47+
await file.FileSystem.File.WriteAllBytesAsync(file.FullName, bytes, cancellationToken);
48+
}
49+
50+
/// <inheritdoc cref="IFile.WriteAllLinesAsync(string,System.Collections.Generic.IEnumerable{string},System.Threading.CancellationToken)" />
51+
public static async Task WriteAllLinesAsync(this IFileInfo file, IEnumerable<string> contents, CancellationToken cancellationToken = default)
52+
{
53+
await file.FileSystem.File.WriteAllLinesAsync(file.FullName, contents, cancellationToken);
54+
}
55+
56+
/// <inheritdoc cref="IFile.WriteAllTextAsync(string,string?,System.Threading.CancellationToken)" />
57+
public static async Task WriteAllTextAsync(this IFileInfo file, string? contents, CancellationToken cancellationToken = default)
58+
{
59+
await file.FileSystem.File.WriteAllTextAsync(file.FullName, contents, cancellationToken);
60+
}
61+
62+
/// <summary>
63+
/// Creates an <see cref="IAsyncEnumerable{String}"/> that can enumerate asynchronously the lines of text in the specified <paramref name="file"/>
64+
/// </summary>
65+
/// <param name="file">File to enumerate content</param>
66+
/// <param name="cancellationToken"></param>
67+
/// <returns>Returns an <see cref="IAsyncEnumerable{String}"/> to enumerate the content of the file</returns>
68+
public static IAsyncEnumerable<string> EnumerateLinesAsync(this IFileInfo file, CancellationToken cancellationToken)
69+
{
70+
return EnumerateLinesAsync(file, null, cancellationToken);
71+
}
72+
73+
/// <summary>
74+
/// Creates an <see cref="IAsyncEnumerable{String}"/> that can enumerate asynchronously the lines of text in the specified <paramref name="file"/>
75+
/// using the specified <paramref name="encoding"/>
76+
/// </summary>
77+
/// <param name="file">File to enumerate content</param>
78+
/// <param name="encoding">Encoding to use to read the file</param>
79+
/// <param name="cancellationToken"></param>
80+
/// <returns>Returns an <see cref="IAsyncEnumerable{String}"/> to enumerate the content of the file</returns>
81+
public static async IAsyncEnumerable<string> EnumerateLinesAsync(this IFileInfo file, Encoding? encoding, [EnumeratorCancellation] CancellationToken cancellationToken)
82+
{
83+
await using var stream = file.OpenRead();
84+
using var reader = encoding == null
85+
? new StreamReader(stream)
86+
: new StreamReader(stream, encoding);
87+
88+
var line = await reader.ReadLineAsync();
89+
while (line != null)
90+
{
91+
yield return line;
92+
cancellationToken.ThrowIfCancellationRequested();
93+
line = await reader.ReadLineAsync();
94+
}
95+
}
96+
}
97+
}
98+
#endif

src/System.IO.Abstractions.Extensions/IFileInfoExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static IEnumerable<string> EnumerateLines(this IFileInfo file)
3131
/// using the specified <paramref name="encoding"/>
3232
/// </summary>
3333
/// <param name="file">File to enumerate content</param>
34+
/// <param name="encoding">Encoding to use to read the file</param>
3435
/// <returns>Returns an <see cref="IEnumerable{String}"/> to enumerate the content of the file</returns>
3536
public static IEnumerable<string> EnumerateLines(this IFileInfo file, Encoding encoding)
3637
{

0 commit comments

Comments
 (0)