Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify File.WriteAllTextAsync #55644

Merged
merged 1 commit into from
Jul 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/libraries/System.Private.CoreLib/src/System/IO/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@ private static async Task InternalWriteAllLinesAsync(TextWriter writer, IEnumera

private static async Task InternalWriteAllTextAsync(StreamWriter sw, string contents, CancellationToken cancellationToken)
{
#if MS_IO_REDIST
char[]? buffer = null;
try
{
Expand All @@ -931,11 +932,7 @@ private static async Task InternalWriteAllTextAsync(StreamWriter sw, string cont
{
int batchSize = Math.Min(DefaultBufferSize, count - index);
contents.CopyTo(index, buffer, 0, batchSize);
#if MS_IO_REDIST
await sw.WriteAsync(buffer, 0, batchSize).ConfigureAwait(false);
#else
await sw.WriteAsync(new ReadOnlyMemory<char>(buffer, 0, batchSize), cancellationToken).ConfigureAwait(false);
#endif
index += batchSize;
}

Expand All @@ -950,6 +947,13 @@ private static async Task InternalWriteAllTextAsync(StreamWriter sw, string cont
ArrayPool<char>.Shared.Return(buffer);
}
}
#else
using (sw)
{
await sw.WriteAsync(contents.AsMemory(), cancellationToken).ConfigureAwait(false);
await sw.FlushAsync().ConfigureAwait(false);
}
#endif
}

public static Task AppendAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default(CancellationToken))
Expand Down