1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
//
// SDLFile.m
// SmartDeviceLink-iOS
//
// Created by Joel Fischer on 10/14/15.
// Copyright © 2015 smartdevicelink. All rights reserved.
//
#import "SDLFile.h"
#import "SDLFileManager.h"
#import "SDLFileType.h"
NS_ASSUME_NONNULL_BEGIN
@interface SDLFile ()
@property (copy, nonatomic, readwrite, nullable) NSURL *fileURL;
@property (copy, nonatomic, readwrite) NSData *data;
@property (strong, nonatomic, readwrite) SDLFileType fileType;
@property (assign, nonatomic, readwrite) BOOL persistent;
@property (copy, nonatomic, readwrite) NSString *name;
@property (nonatomic, readwrite) NSInputStream *inputStream;
@end
@implementation SDLFile
#pragma mark - Lifecycle
- (instancetype)initWithFileURL:(NSURL *)url name:(NSString *)name persistent:(BOOL)persistent {
self = [super init];
if (!self) {
return nil;
}
BOOL exists = url.isFileURL && [url checkResourceIsReachableAndReturnError:nil];
if (!exists) {
return nil;
}
_fileURL = url;
_data = [NSData data];
_name = name;
_persistent = persistent;
_fileType = [self.class sdl_fileTypeFromFileExtension:url.pathExtension];
_overwrite = NO;
return self;
}
+ (instancetype)persistentFileAtFileURL:(NSURL *)url name:(NSString *)name {
return [[self alloc] initWithFileURL:url name:name persistent:YES];
}
+ (instancetype)fileAtFileURL:(NSURL *)url name:(NSString *)name {
return [[self alloc] initWithFileURL:url name:name persistent:NO];
}
- (instancetype)initWithData:(NSData *)data name:(NSString *)name fileExtension:(NSString *)extension persistent:(BOOL)persistent {
self = [super init];
if (!self) {
return nil;
}
if (data.length == 0) {
return nil;
}
_fileURL = nil;
_data = data;
_name = name;
_persistent = persistent;
_fileType = [self.class sdl_fileTypeFromFileExtension:extension];
_overwrite = NO;
return self;
}
+ (instancetype)persistentFileWithData:(NSData *)data name:(NSString *)name fileExtension:(NSString *)extension {
return [[self alloc] initWithData:data name:name fileExtension:extension persistent:YES];
}
+ (instancetype)fileWithData:(NSData *)data name:(NSString *)name fileExtension:(NSString *)extension {
return [[self alloc] initWithData:data name:name fileExtension:extension persistent:NO];
}
#pragma mark - Getters
- (NSData *)data {
if (_data.length == 0 && _fileURL != nil) {
return [NSData dataWithContentsOfURL:_fileURL];
}
return _data;
}
/**
Initalizes a socket from which to read data.
@return A socket
*/
- (NSInputStream *)inputStream {
if (!_inputStream) {
if (_fileURL) {
// Data in file
_inputStream = [[NSInputStream alloc] initWithURL:_fileURL];
} else if (_data.length != 0) {
// Data in memory
_inputStream = [[NSInputStream alloc] initWithData:_data];
}
}
return _inputStream;
}
/**
Gets the size of the data. The data may be stored on disk or it may already be in the application's memory.
@return The size of the data.
*/
- (unsigned long long)fileSize {
if (_fileURL) {
// Data in file
NSString *path = [_fileURL path];
return [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil].fileSize;
} else if (_data) {
// Data in memory
return _data.length;
}
return 0;
}
#pragma mark - File Type
+ (SDLFileType)sdl_fileTypeFromFileExtension:(NSString *)fileExtension {
if ([fileExtension caseInsensitiveCompare:@"bmp"] == NSOrderedSame) {
return SDLFileTypeBMP;
} else if (([fileExtension caseInsensitiveCompare:@"jpg"] == NSOrderedSame) ||
([fileExtension caseInsensitiveCompare:@"jpeg"] == NSOrderedSame)) {
return SDLFileTypeJPEG;
} else if ([fileExtension caseInsensitiveCompare:@"png"] == NSOrderedSame) {
return SDLFileTypePNG;
} else if ([fileExtension caseInsensitiveCompare:@"wav"] == NSOrderedSame) {
return SDLFileTypeWAV;
} else if ([fileExtension caseInsensitiveCompare:@"mp3"] == NSOrderedSame) {
return SDLFileTypeMP3;
} else if ([fileExtension caseInsensitiveCompare:@"aac"] == NSOrderedSame) {
return SDLFileTypeAAC;
} else if ([fileExtension caseInsensitiveCompare:@"json"] == NSOrderedSame) {
return SDLFileTypeJSON;
} else {
return SDLFileTypeBinary;
}
}
#pragma mark - NSCopying
- (id)copyWithZone:(nullable NSZone *)zone {
return [[self.class allocWithZone:zone] initWithFileURL:_fileURL name:_name persistent:_persistent];
}
@end
NS_ASSUME_NONNULL_END
|