diff --git a/Sources/NIOFileSystem/Docs.docc/Extensions/WritableFileHandleProtocol.md b/Sources/NIOFileSystem/Docs.docc/Extensions/WritableFileHandleProtocol.md index f18de129d..74cbda589 100644 --- a/Sources/NIOFileSystem/Docs.docc/Extensions/WritableFileHandleProtocol.md +++ b/Sources/NIOFileSystem/Docs.docc/Extensions/WritableFileHandleProtocol.md @@ -4,7 +4,8 @@ ### Write bytes to a file -- ``write(contentsOf:toAbsoluteOffset:)`` +- ``write(contentsOf:toAbsoluteOffset:)-57fnc`` +- ``write(contentsOf:toAbsoluteOffset:)-4na03`` - ``bufferedWriter(startingAtAbsoluteOffset:capacity:)`` ### Resize a file diff --git a/Sources/NIOFileSystem/FileHandleProtocol.swift b/Sources/NIOFileSystem/FileHandleProtocol.swift index 03044e5a4..372bf0b01 100644 --- a/Sources/NIOFileSystem/FileHandleProtocol.swift +++ b/Sources/NIOFileSystem/FileHandleProtocol.swift @@ -465,6 +465,29 @@ public protocol WritableFileHandleProtocol: FileHandleProtocol { func close(makeChangesVisible: Bool) async throws } +@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) +extension WritableFileHandleProtocol { + /// Write the readable bytes of the `ByteBuffer` to the open file. + /// + /// - 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: + /// - buffer: The bytes to write. + /// - offset: The absolute offset into the file to write the bytes. + /// - Returns: The number of bytes written. + /// - Throws: ``FileSystemError/Code-swift.struct/unsupported`` if file is + /// unseekable and `offset` is not 0. + @discardableResult + public func write( + contentsOf buffer: ByteBuffer, + toAbsoluteOffset offset: Int64 + ) async throws -> Int64 { + try await self.write(contentsOf: buffer.readableBytesView, toAbsoluteOffset: offset) + } +} + /// A file handle which is suitable for reading and writing. @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) public typealias ReadableAndWritableFileHandleProtocol = ReadableFileHandleProtocol diff --git a/Tests/NIOFileSystemIntegrationTests/FileHandleTests.swift b/Tests/NIOFileSystemIntegrationTests/FileHandleTests.swift index 9a5e845d6..5531ea35c 100644 --- a/Tests/NIOFileSystemIntegrationTests/FileHandleTests.swift +++ b/Tests/NIOFileSystemIntegrationTests/FileHandleTests.swift @@ -788,6 +788,30 @@ final class FileHandleTests: XCTestCase { } } + func testWriteByteBuffer() async throws { + try await withTemporaryFile { handle in + let someBytes = ByteBuffer(repeating: 42, count: 1024) + try await handle.write(contentsOf: someBytes, toAbsoluteOffset: 0) + + let readSomeBytes = try await handle.readToEnd(maximumSizeAllowed: .bytes(1024)) + XCTAssertEqual(readSomeBytes, someBytes) + + let moreBytes = ByteBuffer(repeating: 13, count: 1024) + try await handle.write(contentsOf: moreBytes, toAbsoluteOffset: 512) + + let readMoreBytes = try await handle.readToEnd( + fromAbsoluteOffset: 512, + maximumSizeAllowed: .bytes(1024) + ) + XCTAssertEqual(readMoreBytes, moreBytes) + + var allTheBytes = try await handle.readToEnd(maximumSizeAllowed: .bytes(1536)) + let firstBytes = allTheBytes.readSlice(length: 512)! + XCTAssertTrue(firstBytes.readableBytesView.allSatisfy({ $0 == 42 })) + XCTAssertTrue(allTheBytes.readableBytesView.allSatisfy({ $0 == 13 })) + } + } + func testWriteSequenceOfBytes() async throws { try await withTemporaryFile { handle in let byteSequence = UInt8(0)..