Skip to content

Commit

Permalink
Implement append with FileHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhsan committed Jan 16, 2018
1 parent 8e0fdb9 commit 16e0bc5
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,17 @@ public final class File: FileSystem.Item, FileSystemIterable {
* - throws: `File.Error.writeFailed` if the file couldn't be written to
*/
public func append(data: Data) throws {
// TODO: Implement
do {
let handle = try FileHandle(forWritingTo: URL(fileURLWithPath: path))
defer {
handle.closeFile()
}

handle.seekToEndOfFile()
handle.write(data)
} catch {
throw Error.writeFailed
}
}

/**
Expand All @@ -549,7 +559,11 @@ public final class File: FileSystem.Item, FileSystemIterable {
* - throws: `File.Error.writeFailed` if the string couldn't be encoded, or written to the file
*/
public func append(string: String, encoding: String.Encoding = .utf8) throws {
// TODO: Implement
guard let data = string.data(using: encoding) else {
throw Error.writeFailed
}

try append(data: data)
}

/**
Expand Down

0 comments on commit 16e0bc5

Please sign in to comment.