DataFile.swift 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // DataFile.swift
  3. // FileKit
  4. //
  5. // The MIT License (MIT)
  6. //
  7. // Copyright (c) 2015-2017 Nikolai Vazquez
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. //
  27. import Foundation
  28. /// A representation of a filesystem data file.
  29. ///
  30. /// The data type is Data.
  31. open class DataFile: File<Data> {
  32. /// Reads the file and returns its data.
  33. /// - Parameter options: A mask that specifies write options
  34. /// described in `Data.ReadingOptions`.
  35. ///
  36. /// - Throws: `FileKitError.ReadFromFileFail`
  37. /// - Returns: The data read from file.
  38. public func read(_ options: Data.ReadingOptions) throws -> Data {
  39. return try Data.read(from: path, options: options)
  40. }
  41. /// Writes data to the file.
  42. ///
  43. /// - Parameter data: The data to be written to the file.
  44. /// - Parameter options: A mask that specifies write options
  45. /// described in `Data.WritingOptions`.
  46. ///
  47. /// - Throws: `FileKitError.WriteToFileFail`
  48. ///
  49. public func write(_ data: Data, options: Data.WritingOptions) throws {
  50. try data.write(to: self.path, options: options)
  51. }
  52. }
  53. /// A representation of a filesystem data file,
  54. /// with options to read or write.
  55. ///
  56. /// The data type is Data.
  57. open class DataFileWithOptions: DataFile {
  58. open var readingOptions: Data.ReadingOptions = []
  59. open var writingOptions: Data.WritingOptions?
  60. /// Initializes a file from a path with options.
  61. ///
  62. /// - Parameter path: The path to be created a text file from.
  63. /// - Parameter readingOptions: The options to be used to read file.
  64. /// - Parameter writingOptions: The options to be used to write file.
  65. /// If nil take into account `useAuxiliaryFile`
  66. public init(path: Path, readingOptions: Data.ReadingOptions = [], writingOptions: Data.WritingOptions? = nil) {
  67. self.readingOptions = readingOptions
  68. self.writingOptions = writingOptions
  69. super.init(path: path)
  70. }
  71. open override func read() throws -> Data {
  72. return try read(readingOptions)
  73. }
  74. open override func write(_ data: Data, atomically useAuxiliaryFile: Bool) throws {
  75. // If no option take into account useAuxiliaryFile
  76. let options: Data.WritingOptions = (writingOptions == nil) ?
  77. (useAuxiliaryFile ? Data.WritingOptions.atomic : [])
  78. : writingOptions!
  79. try self.write(data, options: options)
  80. }
  81. }