Skip to content

Commit 7474ed7

Browse files
committed
NSData: Improve error handling and new initialiser
Instead of printing out a warning, the underlying file reading function creates proper NSError objects, that are returned to the user. The following initializers were implemented: -[NSData initWithContentsOfFile:options:error:] -[NSData initWithContentsOfURL:options:error:] +[NSData dataWithContentsOfFile:options:error:] +[NSData dataWithContentsOfURL:options:error:] Additionally NSDataReadingOptions was added to NSData.h. Please note that NSDataReadingMappedIfSafe, and NSDataReadingMappedAlways currently have no effect, and need to be implemented properly.
1 parent 131f265 commit 7474ed7

3 files changed

Lines changed: 261 additions & 43 deletions

File tree

Headers/Foundation/NSData.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ extern "C" {
3939
@class NSURL;
4040
#endif
4141

42+
enum {
43+
NSDataReadingMappedIfSafe = 1UL << 0, // Suggests using memory mapping for the file if it can be done securely
44+
NSDataReadingUncached = 1UL << 1, // Suggests avoiding file system caching for the read operation
45+
#if OS_API_VERSION(MAC_OS_X_VERSION_10_7,GS_API_LATEST)
46+
NSDataReadingMappedAlways = 1UL << 3, // Strongly requests memory mapping the file; overrides MappedIfSafe if both are set
47+
#endif
48+
};
49+
typedef NSUInteger NSDataReadingOptions;
50+
4251
#if OS_API_VERSION(MAC_OS_X_VERSION_10_6,GS_API_LATEST)
4352
enum {
4453
NSDataSearchBackwards = (1UL << 0),
@@ -95,9 +104,15 @@ GS_EXPORT_CLASS
95104
length: (NSUInteger)bufferSize
96105
freeWhenDone: (BOOL)shouldFree;
97106
#endif
107+
+ (instancetype)dataWithContentsOfFile: (NSString *)path
108+
options: (NSDataReadingOptions)readOptionsMask
109+
error: (NSError **)errorPtr;
98110
+ (instancetype) dataWithContentsOfFile: (NSString*)path;
99111
+ (instancetype) dataWithContentsOfMappedFile: (NSString*)path;
100112
#if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
113+
+ (instancetype) dataWithContentsOfURL: (NSURL *)url
114+
options: (NSDataReadingOptions)readOptionsMask
115+
error: (NSError **)errorPtr;
101116
+ (instancetype) dataWithContentsOfURL: (NSURL*)url;
102117
#endif
103118
+ (instancetype) dataWithData: (NSData*)data;
@@ -127,9 +142,15 @@ GS_EXPORT_CLASS
127142
freeWhenDone: (BOOL)shouldFree;
128143
#endif
129144
- (instancetype) initWithContentsOfFile: (NSString*)path;
145+
- (instancetype) initWithContentsOfFile:(NSString *) path
146+
options:(NSDataReadingOptions) readOptionsMask
147+
error:(NSError **) errorPtr;
130148
- (instancetype) initWithContentsOfMappedFile: (NSString*)path;
131149
#if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
132150
- (instancetype) initWithContentsOfURL: (NSURL*)url;
151+
- (instancetype) initWithContentsOfURL: (NSURL *)url
152+
options: (NSDataReadingOptions)readOptionsMask
153+
error: (NSError **)errorPtr;
133154
#endif
134155
- (instancetype) initWithData: (NSData*)data;
135156

0 commit comments

Comments
 (0)