Skip to content

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

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

Tyrrrz merged 16 commits into
primefrom
copilot/add-file-extensions-with-async-version

Conversation

Copilot AI commented Apr 12, 2026 •

Copy link
Copy Markdown
Contributor

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 Resolved
Comment thread PowerKit/Extensions/FileExtensions.cs Resolved
Comment thread PowerKit/Extensions/FileExtensions.cs Resolved
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Tyrrrz

Tyrrrz commented Apr 12, 2026

Copy link
Copy Markdown
Owner

@copilot try to fix the build errors

Copilot AI commented Apr 12, 2026

Copy link
Copy Markdown
Contributor Author

@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 Resolved
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 Resolved
Copilot AI requested a review from Tyrrrz April 12, 2026 15:32
Comment thread PowerKit/Extensions/FileExtensions.cs Resolved
Copilot AI requested a review from Tyrrrz April 12, 2026 16:13
@Tyrrrz

Tyrrrz commented Apr 12, 2026

Copy link
Copy Markdown
Owner

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

Copilot AI commented Apr 12, 2026

Copy link
Copy Markdown
Contributor Author

@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

Tyrrrz commented Apr 12, 2026

Copy link
Copy Markdown
Owner

@copilot use the newly introduced TempFile/TestDirectory where needed

Copilot AI commented Apr 12, 2026

Copy link
Copy Markdown
Contributor Author

@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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 Resolved
Comment thread PowerKit/Extensions/FileExtensions.cs Resolved
Comment thread PowerKit/Extensions/FileExtensions.cs Resolved
Comment thread PowerKit/Extensions/FileExtensions.cs Resolved
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