[NIOFileSystem] Provide an API to specify allowing unlimited sized reads (#2914)

Motivation:

As described in issue
[#2877](https://github.com/apple/swift-nio/issues/2877), there is no API
available to specify allowing a read of unlimited size.

Modifications:

- Add a new `public static` property, `unlimited`, to `ByteCount`
representing an unlimited amount of bytes.
- Adapt
`ReadableFileHandleProtocol.readToEnd(fromAbsoluteOffset:maximumSizeAllowed:)`
to work with `maximumSizeAllowed` being `ByteCount.unlimited`.

Result:

An API to specify allowing a read of an unlimited size will be
available.
This commit is contained in:
Clinton Nkwocha authored and GitHub committed 2024-10-11 09:25:17 +00:00
1 parent f6b8f1f94d
commit 17ebfcd99d
5 files changed
+33

No files matched your search

+5
View File
@@ -93,6 +93,11 @@ extension ByteCount {
return ByteCount(bytes: Int64(byteBufferMaxIndex))
}
/// A ``ByteCount`` for an unlimited amount of bytes.
public static var unlimited: ByteCount {
ByteCount(bytes: .max)
}
}
extension ByteCount: AdditiveArithmetic {
@@ -335,6 +335,7 @@ extension ReadableFileHandleProtocol {
fromAbsoluteOffset offset: Int64 = 0,
maximumSizeAllowed: ByteCount
) async throws -> ByteBuffer {
let maximumSizeAllowed = maximumSizeAllowed == .unlimited ? .byteBufferCapacity : maximumSizeAllowed
let info = try await self.info()
let fileSize = Int64(info.size)
let readSize = max(Int(fileSize - offset), 0)