Skip to content

Commit 17ebfcd

Browse files
authored
[NIOFileSystem] Provide an API to specify allowing unlimited sized reads (#2914)
Motivation: As described in issue [#2877](#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.
1 parent f6b8f1f commit 17ebfcd

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

Sources/NIOFileSystem/ByteCount.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ extension ByteCount {
9393

9494
return ByteCount(bytes: Int64(byteBufferMaxIndex))
9595
}
96+
97+
/// A ``ByteCount`` for an unlimited amount of bytes.
98+
public static var unlimited: ByteCount {
99+
ByteCount(bytes: .max)
100+
}
96101
}
97102

98103
extension ByteCount: AdditiveArithmetic {

Sources/NIOFileSystem/FileHandleProtocol.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ extension ReadableFileHandleProtocol {
335335
fromAbsoluteOffset offset: Int64 = 0,
336336
maximumSizeAllowed: ByteCount
337337
) async throws -> ByteBuffer {
338+
let maximumSizeAllowed = maximumSizeAllowed == .unlimited ? .byteBufferCapacity : maximumSizeAllowed
338339
let info = try await self.info()
339340
let fileSize = Int64(info.size)
340341
let readSize = max(Int(fileSize - offset), 0)

Tests/NIOFileSystemIntegrationTests/FileSystemTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,16 @@ extension FileSystemTests {
18181818
}
18191819
}
18201820
}
1821+
1822+
func testReadWithUnlimitedMaximumSizeAllowed() async throws {
1823+
let path = try await self.fs.temporaryFilePath()
1824+
1825+
try await self.fs.withFileHandle(forReadingAndWritingAt: path) { fileHandle in
1826+
await XCTAssertNoThrowAsync(
1827+
try await fileHandle.readToEnd(maximumSizeAllowed: .unlimited)
1828+
)
1829+
}
1830+
}
18211831
}
18221832

18231833
#if !canImport(Darwin) && swift(<5.9.2)

Tests/NIOFileSystemIntegrationTests/XCTestExtensions.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,16 @@ func XCTAssertThrowsFileSystemErrorAsync<R>(
6969
}
7070
}
7171
}
72+
73+
func XCTAssertNoThrowAsync<T>(
74+
_ expression: @autoclosure () async throws -> T,
75+
file: StaticString = #file,
76+
line: UInt = #line
77+
) async {
78+
do {
79+
_ = try await expression()
80+
} catch {
81+
XCTFail("Expression did throw: \(error)", file: file, line: line)
82+
}
83+
}
7284
#endif

Tests/NIOFileSystemTests/ByteCountTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class ByteCountTests: XCTestCase {
5252
XCTAssertEqual(byteCount.bytes, 10_737_418_240)
5353
}
5454

55+
func testByteCountUnlimited() {
56+
let byteCount = ByteCount.unlimited
57+
XCTAssertEqual(byteCount.bytes, .max)
58+
}
59+
5560
func testByteCountEquality() {
5661
let byteCount1 = ByteCount.bytes(10)
5762
let byteCount2 = ByteCount.bytes(20)

0 commit comments

Comments
 (0)