Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NSData initWithContentsOfURL: caching data of file URLs #309

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading