From 0aaa5307c8d18a84bc07b603fa985acd4531582a Mon Sep 17 00:00:00 2001 From: Frederik Seiffert Date: Wed, 26 Jul 2023 21:32:09 +0200 Subject: [PATCH] Fix NSData initWithContentsOfURL: caching data of file URLs --- ChangeLog | 5 +++++ Source/NSData.m | 35 ++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index cdd64908f..eb370e9ea 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2023-07-26 Frederik Seiffert + + * Source/NSData.m: + Fix NSData initWithContentsOfURL: caching data of file URLs. + 2023-07-26 Frederik Seiffert * Source/NSURL.m: diff --git a/Source/NSData.m b/Source/NSData.m index f4fe3105e..d8b714d74 100644 --- a/Source/NSData.m +++ b/Source/NSData.m @@ -643,7 +643,15 @@ + (id) dataWithContentsOfURL: (NSURL*)url { NSData *d; - d = [url resourceDataUsingCache: YES]; + if ([url isFileURL]) + { + d = [dataMalloc allocWithZone: NSDefaultMallocZone()]; + d = AUTORELEASE([d initWithContentsOfFile: [url path]]); + } + else + { + d = [url resourceDataUsingCache: YES]; + } return d; } @@ -955,9 +963,15 @@ - (id) initWithContentsOfMappedFile: (NSString *)path */ - (id) initWithContentsOfURL: (NSURL*)url { - NSData *data = [url resourceDataUsingCache: YES]; - - return [self initWithData: data]; + if ([url isFileURL]) + { + return [self initWithContentsOfFile: [url path]]; + } + else + { + NSData *data = [url resourceDataUsingCache: YES]; + return [self initWithData: data]; + } } /** @@ -2359,11 +2373,18 @@ + (id) dataWithContentsOfMappedFile: (NSString*)path + (id) dataWithContentsOfURL: (NSURL*)url { NSMutableData *d; - NSData *data; d = [mutableDataMalloc allocWithZone: NSDefaultMallocZone()]; - data = [url resourceDataUsingCache: YES]; - d = [d initWithBytes: [data bytes] length: [data length]]; + + if ([url isFileURL]) + { + d = [d initWithContentsOfFile: [url path]]; + } + else + { + NSData *data = [url resourceDataUsingCache: YES]; + d = [d initWithBytes: [data bytes] length: [data length]]; + } return AUTORELEASE(d); }