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

Code before:
    }

    /// Returns an asynchronous sequence of chunks read from the file.
    ///
    /// - Parameters:
    ///   - range: A range of offsets in the file to read.
    ///   - chunkLength: The length of chunks to read, defaults to 128 KiB.
    ///   - as: Type of chunk to read.
    /// - SeeAlso: ``ReadableFileHandleProtocol/readChunks(in:chunkLength:)-2dz6``.
    /// - Returns: An `AsyncSequence` of chunks read from the file.
    public func readChunks(
        chunkLength: ByteCount = .kibibytes(128)
    ) -> FileChunks {
        self.readChunks(in: ..., chunkLength: chunkLength)
    }
}

@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. Also, it will throw
    /// ``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)).
                    """,

Review comment (suggestion):
This past should go in the `Throws:` section below

Code after:
    }

    /// Returns an asynchronous sequence of chunks read from the file.
    ///
    /// - Parameters:
    ///   - range: A range of offsets in the file to read.
    ///   - chunkLength: The length of chunks to read, defaults to 128 KiB.
    ///   - as: Type of chunk to read.
    /// - SeeAlso: ``ReadableFileHandleProtocol/readChunks(in:chunkLength:)-2dz6``.
    /// - Returns: An `AsyncSequence` of chunks read from the file.
    public func readChunks(
        chunkLength: ByteCount = .kibibytes(128)
    ) -> FileChunks {
        self.readChunks(in: ..., chunkLength: chunkLength)
    }
}

@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 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()