Repository: apple/swift-nio
File: Sources/NIOFileSystem/FileHandleProtocol.swift (Swift)

Code before:
    }
}

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension ReadableFileHandleProtocol {
    /// Returns the contents of a file by loading it into memory.
    ///
    /// - Important: This method checks whether the file is seekable or not (i.e., whether it's a socket,
    /// pipe or FIFO), and will throw ``FileSystemError/Code-swift.struct/unsupported`` if
    /// an offset other than zero is passed.
    ///
    /// - Parameters:
    ///   - offset: The absolute offset into the file to read from. Defaults to zero.
    ///   - maximumSizeAllowed: The maximum size of file to read, as a ``ByteCount``.
    /// - Returns: The bytes read from the file.
    /// - Throws: ``FileSystemError`` with code ``FileSystemError/Code-swift.struct/resourceExhausted``
    /// if `maximumSizeAllowed` is more than can be written to `ByteBuffer`. Or if there are more bytes to read than
    /// `maximumBytesAllowed`.
    public func readToEnd(
        fromAbsoluteOffset offset: Int64 = 0,
        maximumSizeAllowed: ByteCount
    ) async throws -> ByteBuffer {
        var maximumSizeAllowed = maximumSizeAllowed
        if maximumSizeAllowed == .unlimited {
            maximumSizeAllowed = .byteBufferCapacity
        }

        let info = try await self.info()
        let fileSize = Int64(info.size)
        let readSize = max(Int(fileSize - offset), 0)

        if maximumSizeAllowed > .byteBufferCapacity {
            throw FileSystemError(
                code: .resourceExhausted,
                message: """
                    The maximum size allowed (\(maximumSizeAllowed)) is more than the maximum \
                    amount of bytes that can be written to ByteBuffer \
                    (\(ByteCount.byteBufferCapacity)). You can read the file in smaller chunks by \
                    calling readChunks().
                    """,
                cause: nil,
                location: .here()
            )
        }

        if readSize > maximumSizeAllowed.bytes {
            throw FileSystemError(
                code: .resourceExhausted,
                message: """
                    There are more bytes to read (\(readSize)) than the maximum size allowed \
                    (\(maximumSizeAllowed)). Read the file in chunks or increase the maximum size \

Review comment (suggestion):
We can do this in a one line to keep `maximumSizeAllowed` constant:

```swift
let maximumSizeAllowed = maximumSizeAllowed == .unlimited ? .byteBufferCapacity : maximumSizeAllowed
```

Code after:
    }
}

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension ReadableFileHandleProtocol {
    /// Returns the contents of a file by loading it into memory.
    ///
    /// - Important: This method checks whether the file is seekable or not (i.e., whether it's a socket,
    /// pipe or FIFO), and will throw ``FileSystemError/Code-swift.struct/unsupported`` if
    /// an offset other than zero is passed.
    ///
    /// - Parameters:
    ///   - offset: The absolute offset into the file to read from. Defaults to zero.
    ///   - maximumSizeAllowed: The maximum size of file to read, as a ``ByteCount``.
    /// - Returns: The bytes read from the file.
    /// - Throws: ``FileSystemError`` with code ``FileSystemError/Code-swift.struct/resourceExhausted``
    /// if `maximumSizeAllowed` is more than can be written to `ByteBuffer`. Or if there are more bytes to read than
    /// `maximumBytesAllowed`.
    public func readToEnd(
        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)

        if maximumSizeAllowed > .byteBufferCapacity {
            throw FileSystemError(
                code: .resourceExhausted,
                message: """
                    The maximum size allowed (\(maximumSizeAllowed)) is more than the maximum \
                    amount of bytes that can be written to ByteBuffer \
                    (\(ByteCount.byteBufferCapacity)). You can read the file in smaller chunks by \
                    calling readChunks().
                    """,
                cause: nil,
                location: .here()
            )
        }

        if readSize > maximumSizeAllowed.bytes {
            throw FileSystemError(
                code: .resourceExhausted,
                message: """
                    There are more bytes to read (\(readSize)) than the maximum size allowed \
                    (\(maximumSizeAllowed)). Read the file in chunks or increase the maximum size \
                    allowed.
                    """,
                cause: nil,
                location: .here()