-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.IOin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged
Milestone
Description
EDITED by @stephentoub on 4/21/2024:
Exact same shape as existing APIs that take byte[]/string, just taking ReadOnlySpan<byte/char> instead for sync and ReadOnlyMemory<byte/char> for async.
public static class File
{
+ public static void WriteAllBytes(string path, ReadOnlySpan<byte> bytes);
+ public static Task WriteAllBytesAsync(string path, ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default);
+ public static void WriteAllText(string path, ReadOnlySpan<char> contents);
+ public static void WriteAllText(string path, ReadOnlySpan<char> contents, Encoding encoding);
+ public static Task WriteAllTextAsync(string path, ReadOnlyMemory<byte> contents, CancellationToken cancellationToken = default);
+ public static Task WriteAllTextAsync(string path, ReadOnlyMemory<byte> contents, Encoding encoding, CancellationToken cancellationToken = default);
+ public static void AppendAllBytes(string path, ReadOnlySpan<byte> bytes);
+ public static Task AppendAllBytesAsync(string path, ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default);
+ public static void AppendAllText(string path, ReadOnlySpan<char> contents);
+ public static void AppendAllText(string path, ReadOnlySpan<char> contents, Encoding encoding);
+ public static Task AppendAllTextAsync(string path, ReadOnlyMemory<char> contents, CancellationToken cancellationToken = default);
+ public static Task AppendAllTextAsync(string path, ReadOnlyMemory<char> contents, Encoding encoding, CancellationToken cancellationToken = default);
}Background and motivation
Today, File.ReadAllBytes takes byte[] representing the bytes. It would be good to have an overload that takes ReadOnlySpan (sync ReadAllBytes) and ReadOnlyMemory (async ReadAllBytesAsync)
API Proposal
namespace System.IO;
public static class File
{
public static void WriteAllBytes(string path, ReadOnlySpan<byte> bytes);
public static void WriteAllBytes(string path, ReadOnlyMemory<byte> bytes);
public static Task WriteAllBytesAsync(string path, ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default);
public static void AppendAllBytes(string path, ReadOnlySpan<byte> bytes);
public static void AppendAllBytes(string path, ReadOnlyMemory<byte> bytes);
public static Task AppendAllBytesAsync(string path, ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default);
}API Usage
File.WriteAllBytes(path, "Hello World!"u8);Alternative Designs
Risks
No response
PaulusParssinen, tannergooding, omariom, neon-sunset, nil4 and 4 more
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.IOin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged