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

Code before:
    /// ``FileSystemError/Code-swift.struct/resourceExhausted`` if `maximumSizeAllowed` is more than can be
    /// written to `ByteBuffer`.
    ///
    /// - 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 there
    ///     are more bytes to read than `maximumBytesAllowed`.
    ///     ``FileSystemError/Code-swift.struct/unsupported`` if file is unseekable and
    ///     `offset` is not 0.
    public func readToEnd(
        fromAbsoluteOffset offset: Int64 = 0,
        maximumSizeAllowed: ByteCount
    ) async throws -> ByteBuffer {
        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)).
                    """,
                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()
            )
        }

        let isSeekable = !(info.type == .fifo || info.type == .socket)
        if isSeekable {
            // Limit the size of single shot reads. If the system is busy then avoid slowing down other
            // work by blocking an I/O executor thread while doing a large read. The limit is somewhat
            // arbitrary.
            let singleShotReadLimit = 64 * 1024 * 1024

Review comment (suggestion):
```suggestion
                    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().
```

Code after:
    ///
    /// - 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 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()
            )
        }

        let isSeekable = !(info.type == .fifo || info.type == .socket)
        if isSeekable {
            // Limit the size of single shot reads. If the system is busy then avoid slowing down other
            // work by blocking an I/O executor thread while doing a large read. The limit is somewhat
            // arbitrary.
            let singleShotReadLimit = 64 * 1024 * 1024

            // If the file size isn't 0 but the read size is, then it means that