87 lines
3.6 KiB
Swift
87 lines
3.6 KiB
Swift
//
|
|
// Data+Extensions.swift
|
|
// FluxRead
|
|
//
|
|
// Created by RSA on 15/02/2026.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension Data {
|
|
|
|
init<T>(copyingBufferOf array: [T]) {
|
|
self = array.withUnsafeBufferPointer(Data.init)
|
|
}
|
|
|
|
func toArray<T>(type: T.Type) -> [T] where T: ExpressibleByIntegerLiteral {
|
|
var array = [T](repeating: 0, count: self.count / MemoryLayout<T>.stride)
|
|
_ = array.withUnsafeMutableBytes { copyBytes(to: $0) }
|
|
return array
|
|
}
|
|
|
|
/// Saves the Data object to a file at the specified URL
|
|
/// - Parameters:
|
|
/// - url: The destination file URL
|
|
/// - options: Writing options (default: .atomic)
|
|
/// - Throws: Error if writing fails
|
|
func saveToFile(at url: URL, options: WritingOptions = .atomic) throws {
|
|
try self.write(to: url, options: options)
|
|
|
|
}
|
|
|
|
/// Saves the Data object to a file with a specified filename in a directory
|
|
/// - Parameters:
|
|
/// - filename: The name of the file to create
|
|
/// - directory: The directory URL where the file should be saved
|
|
/// - options: Writing options (default: .atomic)
|
|
/// - Returns: The URL of the saved file
|
|
/// - Throws: Error if writing fails
|
|
@discardableResult
|
|
func saveToFile(filename: String, in directory: URL, options: WritingOptions = .atomic) throws -> URL {
|
|
let fileURL = directory.appendingPathComponent(filename)
|
|
try self.write(to: fileURL, options: options)
|
|
|
|
return fileURL
|
|
}
|
|
|
|
/// Saves the Data object to the app's Documents directory
|
|
/// - Parameters:
|
|
/// - filename: The name of the file to create
|
|
/// - options: Writing options (default: .atomic)
|
|
/// - Returns: The URL of the saved file
|
|
/// - Throws: Error if writing fails or Documents directory not accessible
|
|
@discardableResult
|
|
func saveToDocuments(filename: String, options: WritingOptions = .atomic) throws -> URL {
|
|
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
|
|
throw NSError(domain: "DataExtension", code: -1, userInfo: [NSLocalizedDescriptionKey: "Could not access Documents directory"])
|
|
}
|
|
return try saveToFile(filename: filename, in: documentsDirectory, options: options)
|
|
}
|
|
|
|
/// Saves the Data object to the app's temporary directory
|
|
/// - Parameters:
|
|
/// - filename: The name of the file to create
|
|
/// - options: Writing options (default: .atomic)
|
|
/// - Returns: The URL of the saved file
|
|
/// - Throws: Error if writing fails
|
|
@discardableResult
|
|
func saveToTemporary(filename: String, options: WritingOptions = .atomic) throws -> URL {
|
|
let temporaryDirectory = FileManager.default.temporaryDirectory
|
|
return try saveToFile(filename: filename, in: temporaryDirectory, options: options)
|
|
}
|
|
|
|
/// Saves the Data object to the app's Caches directory
|
|
/// - Parameters:
|
|
/// - filename: The name of the file to create
|
|
/// - options: Writing options (default: .atomic)
|
|
/// - Returns: The URL of the saved file
|
|
/// - Throws: Error if writing fails or Caches directory not accessible
|
|
@discardableResult
|
|
func saveToCaches(filename: String, options: WritingOptions = .atomic) throws -> URL {
|
|
guard let cachesDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
|
|
throw NSError(domain: "DataExtension", code: -1, userInfo: [NSLocalizedDescriptionKey: "Could not access Caches directory"])
|
|
}
|
|
return try saveToFile(filename: filename, in: cachesDirectory, options: options)
|
|
}
|
|
}
|