@@ -185,31 +185,36 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
185185 /// Initializes a data object with the data from the location specified by a given URL.
186186 public init ( contentsOf url: URL , options readOptionsMask: ReadingOptions = [ ] ) throws {
187187 super. init ( )
188- try _contentsOf ( url: url, options: readOptionsMask)
188+ let ( data, _) = try NSData . contentsOf ( url: url, options: readOptionsMask)
189+ _init ( bytes: UnsafeMutableRawPointer ( mutating: data. bytes) , length: data. length, copy: true )
189190 }
190191
191192 /// Initializes a data object with the data from the location specified by a given URL.
192193 public init ? ( contentsOf url: URL ) {
193194 super. init ( )
194195 do {
195- try _contentsOf ( url: url)
196+ let ( data, _) = try NSData . contentsOf ( url: url)
197+ _init ( bytes: UnsafeMutableRawPointer ( mutating: data. bytes) , length: data. length, copy: true )
196198 } catch {
197199 return nil
198200 }
199201 }
200202
201- /// Initializes a data object with the data from the location specified by a given URL.
202- private func _contentsOf( url: URL , options readOptionsMask: ReadingOptions = [ ] ) throws {
203+ internal static func contentsOf( url: URL , options readOptionsMask: ReadingOptions = [ ] ) throws -> ( NSData , URLResponse ? ) {
204+ let readResult : NSData
205+ var urlResponse : URLResponse ?
206+
203207 if url. isFileURL {
204- let readResult = try NSData . readBytesFromFileWithExtendedAttributes ( url. path, options: readOptionsMask)
205- _init ( bytes : readResult . bytes, length: readResult . length, copy : false , deallocator: readResult . deallocator)
208+ let data = try NSData . readBytesFromFileWithExtendedAttributes ( url. path, options: readOptionsMask)
209+ readResult = NSData ( bytesNoCopy : data . bytes, length: data . length, deallocator: data . deallocator)
206210 } else {
207211 let session = URLSession ( configuration: URLSessionConfiguration . default)
208212 let cond = NSCondition ( )
209213 var resError : Error ?
210214 var resData : Data ?
211215 let task = session. dataTask ( with: url, completionHandler: { data, response, error in
212216 resData = data
217+ urlResponse = response
213218 resError = error
214219 cond. broadcast ( )
215220 } )
@@ -218,8 +223,9 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
218223 guard let data = resData else {
219224 throw resError!
220225 }
221- _init ( bytes: UnsafeMutableRawPointer ( mutating: data. _nsObject. bytes) , length: data. count, copy : true )
226+ readResult = NSData ( bytes: UnsafeMutableRawPointer ( mutating: data. _nsObject. bytes) , length: data. count)
222227 }
228+ return ( readResult, urlResponse)
223229 }
224230
225231 /// Initializes a data object with the given Base64 encoded string.
0 commit comments