Skip to content

Add File extensions: WriteAllZeroes, ReadAllBytes(offset), ReadAllBytes(offset, length), ReadAllBytesAsync(offset), ReadAllBytesAsync(offset, length)#8

Merged
Tyrrrz merged 16 commits intoprimefrom
copilot/add-file-extensions-with-async-version
Apr 12, 2026
Merged

Add File extensions: WriteAllZeroes, ReadAllBytes(offset), ReadAllBytes(offset, length), ReadAllBytesAsync(offset), ReadAllBytesAsync(offset, length)#8
Tyrrrz merged 16 commits intoprimefrom
copilot/add-file-extensions-with-async-version

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 12, 2026

Ports the File static-class extensions from GitHubActionsTestLogger, adding an async counterpart for the read operation.

New extensions

  • File.WriteAllZeroes(path, count) — creates/overwrites a file of exactly count zero bytes via SetLength (no async; it's a metadata-only syscall)
  • File.ReadAllBytes(path, offset) — reads all bytes from offset (a long) to EOF; returns an empty array when offset >= file length
  • File.ReadAllBytes(path, offset, length) — reads exactly length bytes starting at offset
  • File.ReadAllBytesAsync(path, offset, cancellationToken) — same as the offset-only sync version, using ReadExactlyAsync
  • File.ReadAllBytesAsync(path, offset, length, cancellationToken) — same as the offset+length sync version, using ReadExactlyAsync

The length parameter in the offset+length overloads is int (matching the array size constraint), and offset is long to support large files.

File.WriteAllZeroes(path, count: 1024);

byte[] tail = File.ReadAllBytes(path, offset: 512L);
byte[] slice = File.ReadAllBytes(path, offset: 512L, length: 256);

byte[] tail = await File.ReadAllBytesAsync(path, offset: 512L);
byte[] slice = await File.ReadAllBytesAsync(path, offset: 512L, length: 256);

@Tyrrrz Tyrrrz added the enhancement New feature or request label Apr 12, 2026
@Tyrrrz Tyrrrz marked this pull request as ready for review April 12, 2026 13:42
Copilot AI review requested due to automatic review settings April 12, 2026 13:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Ports a small set of System.IO.File extension members into PowerKit to support creating zero-filled files and reading a file tail from a given offset (sync + async), with accompanying unit tests.

Changes:

  • Added File.WriteAllZeroes(path, count) to create/truncate a file to a specific length.
  • Added File.ReadAllBytes(path, offset) and File.ReadAllBytesAsync(path, offset, cancellationToken) to read from an offset to EOF.
  • Added xUnit coverage for the new extensions.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
PowerKit/Extensions/FileExtensions.cs Introduces the new File extension members (write-zeroes + read-from-offset sync/async).
PowerKit.Tests/FileExtensionsTests.cs Adds tests validating zero-fill size/content and read-from-offset behavior for both sync and async APIs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread PowerKit/Extensions/FileExtensions.cs
Comment thread PowerKit/Extensions/FileExtensions.cs
Comment thread PowerKit/Extensions/FileExtensions.cs Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Tyrrrz
Copy link
Copy Markdown
Owner

Tyrrrz commented Apr 12, 2026

@copilot try to fix the build errors

…st long to int

Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/42b39082-bced-464c-a7ec-e441e8191260

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 12, 2026

@copilot try to fix the build errors

Fixed in 1663629. Two issues were causing the build failures:

  1. FileStreamOptions isn't available in netstandard2.0 — replaced with the 6-parameter FileStream constructor that accepts FileOptions.Asynchronous directly.
  2. stream.Length - offset produces a long which can't be used as an array length implicitly — wrapped in checked((int)(...)) so it converts safely and throws OverflowException if the remaining length exceeds int.MaxValue.

Copilot AI requested a review from Tyrrrz April 12, 2026 13:53
Comment thread PowerKit/Extensions/FileExtensions.cs Outdated
Copilot AI changed the title Add File extensions: WriteAllZeroes, ReadAllBytes(offset), ReadAllBytesAsync(offset) Add File extensions: WriteAllZeroes, ReadAllBytes(offset), ReadAllBytes(offset, length), ReadAllBytesAsync(offset), ReadAllBytesAsync(offset, length) Apr 12, 2026
Copilot AI requested a review from Tyrrrz April 12, 2026 15:20
Comment thread PowerKit/Extensions/FileExtensions.cs
Copilot AI requested a review from Tyrrrz April 12, 2026 15:32
Comment thread PowerKit/Extensions/FileExtensions.cs Outdated
…t+length overloads

Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/b2d744a2-3045-4163-9063-ee30228ef2ba

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copilot AI requested a review from Tyrrrz April 12, 2026 16:13
@Tyrrrz
Copy link
Copy Markdown
Owner

Tyrrrz commented Apr 12, 2026

@copilot let's make length an int so it fits into an array?

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 12, 2026

@copilot let's make length an int so it fits into an array?

Done in 7312aff. Both ReadAllBytes(path, offset, length) and ReadAllBytesAsync(path, offset, length, …) now take int length. The guard is simplified to just length < 0, and the buffer allocation is new byte[length] directly (no cast needed).

@Tyrrrz
Copy link
Copy Markdown
Owner

Tyrrrz commented Apr 12, 2026

@copilot use the newly introduced TempFile/TestDirectory where needed

…ath + try/finally

Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/dd4b70b5-e3e1-49a9-aacf-72d58178048a

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 12, 2026

@copilot use the newly introduced TempFile/TestDirectory where needed

Done in 33caa32. All nine tests now use using var tempFile = TempFile.Create() instead of Path.GetTempFileName() + try/finally blocks.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread PowerKit/Extensions/FileExtensions.cs
Comment thread PowerKit/Extensions/FileExtensions.cs
Comment thread PowerKit/Extensions/FileExtensions.cs
Comment thread PowerKit/Extensions/FileExtensions.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants