Skip to content

Commit

Permalink
Fix NSData initWithContentsOfURL: caching data of file URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
triplef committed Jul 26, 2023
1 parent 48c8a1a commit 2b5e054
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2023-07-26 Frederik Seiffert <[email protected]>

* Source/NSData.m:
Fix NSData initWithContentsOfURL: caching data of file URLs.

2023-07-26 Frederik Seiffert <[email protected]>

* Source/NSURL.m:
Expand Down
35 changes: 28 additions & 7 deletions Source/NSData.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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];
}
}

/**
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 2b5e054

Please sign in to comment.