10 #pragma mark - Codec for basic message channel
15 + (instancetype)sharedInstance {
16 static id _sharedInstance = nil;
17 if (!_sharedInstance) {
21 return _sharedInstance;
30 NSAssert(
self,
@"Super init cannot be nil");
31 _readerWriter = readerWriter;
35 - (NSData*)encode:(
id)message {
39 NSMutableData* data = [NSMutableData dataWithCapacity:32];
45 - (id)decode:(NSData*)message {
46 if ([message length] == 0) {
51 NSAssert(![reader hasMore],
@"Corrupted standard message");
56 #pragma mark - Codec for method channel
61 + (instancetype)sharedInstance {
62 static id _sharedInstance = nil;
63 if (!_sharedInstance) {
67 return _sharedInstance;
76 NSAssert(
self,
@"Super init cannot be nil");
77 _readerWriter = readerWriter;
82 NSMutableData* data = [NSMutableData dataWithCapacity:32];
89 - (NSData*)encodeSuccessEnvelope:(
id)result {
90 NSMutableData* data = [NSMutableData dataWithCapacity:32];
98 NSMutableData* data = [NSMutableData dataWithCapacity:32];
111 NSAssert(![reader hasMore],
@"Corrupted standard method call");
112 NSAssert([value1 isKindOfClass:[NSString
class]],
@"Corrupted standard method call");
116 - (id)decodeEnvelope:(NSData*)envelope {
119 NSAssert(flag <= 1,
@"Corrupted standard envelope");
124 NSAssert(![reader hasMore],
@"Corrupted standard envelope");
130 NSAssert(![reader hasMore],
@"Corrupted standard envelope");
131 NSAssert([code isKindOfClass:[NSString
class]],
@"Invalid standard envelope");
132 NSAssert(message == nil || [message isKindOfClass:[NSString
class]],
133 @"Invalid standard envelope");
143 #pragma mark - Standard serializable types
146 + (instancetype)typedDataWithBytes:(NSData*)data {
150 + (instancetype)typedDataWithInt32:(NSData*)data {
154 + (instancetype)typedDataWithInt64:(NSData*)data {
158 + (instancetype)typedDataWithFloat32:(NSData*)data {
162 + (instancetype)typedDataWithFloat64:(NSData*)data {
166 + (instancetype)typedDataWithData:(NSData*)data type:(FlutterStandardDataType)type {
170 - (instancetype)initWithData:(NSData*)data type:(FlutterStandardDataType)type {
172 NSAssert(
data,
@"Data cannot be nil");
173 NSAssert(
data.length %
elementSize == 0,
@"Data must contain integral number of elements");
175 NSAssert(
self,
@"Super init cannot be nil");
183 - (BOOL)isEqual:(
id)object {
184 if (
self ==
object) {
192 [
self.data isEqual:other.data];
196 return [
self.data hash] ^
self.type;
200 #pragma mark - Writer and reader of standard codec
203 NSMutableData* _data;
206 - (instancetype)initWithData:(NSMutableData*)data {
208 NSAssert(
self,
@"Super init cannot be nil");
213 - (void)writeByte:(UInt8)value {
217 - (void)writeBytes:(const
void*)bytes length:(NSUInteger)length {
221 - (void)writeData:(NSData*)data {
225 - (void)writeSize:(UInt32)size {
229 - (void)writeAlignment:(UInt8)alignment {
233 - (void)writeUTF8:(NSString*)value {
235 (__bridge CFStringRef)value);
239 if (value == nil || (__bridge CFNullRef)value == kCFNull) {
241 }
else if ([value isKindOfClass:[NSNumber
class]]) {
243 }
else if ([value isKindOfClass:[NSString
class]]) {
247 }
else if ([value isKindOfClass:[NSData
class]]) {
249 }
else if ([value isKindOfClass:[NSArray
class]]) {
251 }
else if ([value isKindOfClass:[NSDictionary
class]]) {
263 static void WriteKeyValues(CFTypeRef key, CFTypeRef value,
void* context) {
265 FastWriteValueOfType(info->
writer, info->
data, key);
266 FastWriteValueOfType(info->
writer, info->
data, value);
271 static void FastWriteValueOfType(CFTypeRef writer, CFMutableDataRef data, CFTypeRef value) {
274 WriteValueOfType(writer, data, type, value);
280 static void WriteValueOfType(CFTypeRef writer,
281 CFMutableDataRef data,
289 CFNumberRef number = (CFNumberRef)value;
292 NSLog(
@"Unsupported value: %@ of number type %ld", value, CFNumberGetType(number));
293 NSCAssert(NO,
@"Unsupported value for standard codec");
298 CFStringRef
string = (CFStringRef)value;
317 CFArrayRef array = (CFArrayRef)value;
319 CFIndex count = CFArrayGetCount(array);
321 for (CFIndex i = 0; i < count; ++i) {
322 FastWriteValueOfType(writer, data, CFArrayGetValueAtIndex(array, i));
327 CFDictionaryRef dict = (CFDictionaryRef)value;
329 CFIndex count = CFDictionaryGetCount(dict);
335 CFDictionaryApplyFunction(dict, WriteKeyValues, (
void*)&info);
339 id objc_value = (__bridge id)value;
340 NSLog(
@"Unsupported value: %@ of type %@", objc_value, [objc_value
class]);
341 NSCAssert(NO,
@"Unsupported value for standard codec");
347 - (void)writeValue:(
id)value {
349 WriteValueOfType((__bridge CFTypeRef)
self, (__bridge CFMutableDataRef)self->_data, type,
350 (__bridge CFTypeRef)value);
359 - (instancetype)initWithData:(NSData*)data {
361 NSAssert(
self,
@"Super init cannot be nil");
363 _range = NSMakeRange(0, 0);
368 return _range.location < _data.length;
371 - (void)readBytes:(
void*)destination length:(NSUInteger)length {
373 (__bridge CFDataRef)_data);
384 - (NSData*)readData:(NSUInteger)length {
386 NSData* data = [_data subdataWithRange:_range];
391 - (NSString*)readUTF8 {
393 (__bridge CFDataRef)_data);
396 - (void)readAlignment:(UInt8)alignment {
400 - (nullable id)readValue {
401 return (__bridge
id)ReadValue((__bridge CFTypeRef)
self);
404 static CFTypeRef ReadValue(CFTypeRef
user_data) {
407 (__bridge CFDataRef)reader->_data);
413 unsigned long* location = &reader->_range.location;
414 CFDataRef data = (__bridge CFDataRef)reader->_data;
420 UInt64 length = elementCount * elementSize;
421 NSRange range = NSMakeRange(*location, length);
424 NSData* bytes = [(__bridge NSData*)data subdataWithRange:range];
429 - (nullable id)readValueOfType:(UInt8)type {
431 &
_range.location, (__bridge CFDataRef)_data, type, ReadValue, ReadTypedDataOfType,
432 (__bridge CFTypeRef)
self);