Skip to content

[API Proposal]: Add File.WriteAllBytes taking ReadOnlySpan<byte> #99823

@KrzysztofCwalina

Description

@KrzysztofCwalina

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

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.IOin-prThere is an active PR which will close this issue when it is merged

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions