66
77#pragma mark - Codec for basic message channel
88
9- static const UInt8 kZeroBuffer [8 ] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
10- // Classes are cached in static variables to avoid the extra method calls in a
11- // highly traffic'd recursive function.
12- static const Class kNSNumberClass = [NSNumber class ];
13- static const id kNSNull = [NSNull null ];
14- static const Class kNSStringClass = [NSString class ];
15- static const Class kNSDataClass = [NSData class ];
16- static const Class kNSArrayClass = [NSArray class ];
17- static const Class kNSDictionaryClass = [NSDictionary class ];
18- static const Class kFlutterStandardTypedDataClass = [FlutterStandardTypedData class ];
19-
209@implementation FlutterStandardMessageCodec {
2110 FlutterStandardReaderWriter* _readerWriter;
2211}
@@ -232,142 +221,115 @@ - (void)dealloc {
232221 [super dealloc ];
233222}
234223
235- static void WriteByte (CFMutableDataRef data, UInt8 value) {
236- CFDataAppendBytes (data, &value, 1 ) ;
224+ - ( void ) writeByte : ( UInt8) value {
225+ [_data appendBytes: &value length: 1 ] ;
237226}
238227
239- static void WriteBytes (CFMutableDataRef data, const void * bytes, NSUInteger length) {
240- CFDataAppendBytes (data, ( const UInt8*) bytes, length) ;
228+ - ( void ) writeBytes : ( const void *) bytes length : ( NSUInteger ) length {
229+ [_data appendBytes: bytes length: length] ;
241230}
242231
243- static void WriteData (CFMutableDataRef destination, NSData * source) {
244- CFDataAppendBytes (destination, ( const UInt8*)source. bytes , source. length ) ;
232+ - ( void ) writeData : ( NSData *) data {
233+ [_data appendData: data] ;
245234}
246235
247- static void WriteSize (CFMutableDataRef data, UInt32 size) {
236+ - ( void ) writeSize : ( UInt32) size {
248237 if (size < 254 ) {
249- WriteByte (data, (UInt8)size) ;
238+ [ self writeByte: (UInt8)size] ;
250239 } else if (size <= 0xffff ) {
251- WriteByte (data, 254 ) ;
240+ [ self writeByte: 254 ] ;
252241 UInt16 value = (UInt16)size;
253- WriteBytes (data, &value, 2 ) ;
242+ [ self writeBytes: &value length: 2 ] ;
254243 } else {
255- WriteByte (data, 255 ) ;
256- WriteBytes (data, &size, 4 ) ;
244+ [ self writeByte: 255 ] ;
245+ [ self writeBytes: &size length: 4 ] ;
257246 }
258247}
259248
260- static void WriteAlignment (CFMutableDataRef data, UInt8 alignment) {
261- NSCAssert (alignment <= 8 , @" Alignment larger than kZeroBuffer." );
262- UInt8 mod = CFDataGetLength (data) % alignment;
249+ - (void )writeAlignment : (UInt8)alignment {
250+ UInt8 mod = _data.length % alignment;
263251 if (mod) {
264- WriteBytes (data, kZeroBuffer , alignment - mod);
252+ for (int i = 0 ; i < (alignment - mod); i++) {
253+ [self writeByte: 0 ];
254+ }
265255 }
266256}
267257
268- static void WriteUTF8 (CFMutableDataRef data, NSString * value) {
258+ - ( void ) writeUTF8 : ( NSString *) value {
269259 UInt32 length = [value lengthOfBytesUsingEncoding: NSUTF8StringEncoding];
270- WriteSize (data, length) ;
271- WriteBytes (data, value.UTF8String , length) ;
260+ [ self writeSize: length] ;
261+ [ self writeBytes: value.UTF8String length: length] ;
272262}
273263
274- static void WriteValue (CFMutableDataRef data, id value) {
275- if (value == nil || value == kNSNull ) {
276- WriteByte (data, FlutterStandardFieldNil) ;
277- } else if ([value isKindOfClass: kNSNumberClass ]) {
264+ - ( void ) writeValue : ( id ) value {
265+ if (value == nil || value == [ NSNull null ] ) {
266+ [ self writeByte: FlutterStandardFieldNil] ;
267+ } else if ([value isKindOfClass: [ NSNumber class ] ]) {
278268 CFNumberRef number = (CFNumberRef)value;
279269 BOOL success = NO ;
280270 if (CFGetTypeID (number) == CFBooleanGetTypeID ()) {
281271 BOOL b = CFBooleanGetValue ((CFBooleanRef)number);
282- WriteByte (data, (b ? FlutterStandardFieldTrue : FlutterStandardFieldFalse)) ;
272+ [ self writeByte: (b ? FlutterStandardFieldTrue : FlutterStandardFieldFalse)] ;
283273 success = YES ;
284274 } else if (CFNumberIsFloatType (number)) {
285275 Float64 f;
286276 success = CFNumberGetValue (number, kCFNumberFloat64Type , &f);
287277 if (success) {
288- WriteByte (data, FlutterStandardFieldFloat64) ;
289- WriteAlignment (data, 8 ) ;
290- WriteBytes (data, (UInt8*)&f, 8 ) ;
278+ [ self writeByte: FlutterStandardFieldFloat64] ;
279+ [ self writeAlignment: 8 ] ;
280+ [ self writeBytes: (UInt8*)&f length: 8 ] ;
291281 }
292282 } else if (CFNumberGetByteSize (number) <= 4 ) {
293283 SInt32 n;
294284 success = CFNumberGetValue (number, kCFNumberSInt32Type , &n);
295285 if (success) {
296- WriteByte (data, FlutterStandardFieldInt32) ;
297- WriteBytes (data, (UInt8*)&n, 4 ) ;
286+ [ self writeByte: FlutterStandardFieldInt32] ;
287+ [ self writeBytes: (UInt8*)&n length: 4 ] ;
298288 }
299289 } else if (CFNumberGetByteSize (number) <= 8 ) {
300290 SInt64 n;
301291 success = CFNumberGetValue (number, kCFNumberSInt64Type , &n);
302292 if (success) {
303- WriteByte (data, FlutterStandardFieldInt64) ;
304- WriteBytes (data, (UInt8*)&n, 8 ) ;
293+ [ self writeByte: FlutterStandardFieldInt64] ;
294+ [ self writeBytes: (UInt8*)&n length: 8 ] ;
305295 }
306296 }
307297 if (!success) {
308298 NSLog (@" Unsupported value: %@ of number type %ld " , value, CFNumberGetType(number));
309- NSCAssert (NO , @" Unsupported value for standard codec. " );
299+ NSAssert (NO , @" Unsupported value for standard codec" );
310300 }
311- } else if ([value isKindOfClass: kNSStringClass ]) {
301+ } else if ([value isKindOfClass: [ NSString class ] ]) {
312302 NSString * string = value;
313- WriteByte (data, FlutterStandardFieldString) ;
314- WriteUTF8 (data, string) ;
315- } else if ([value isKindOfClass: kFlutterStandardTypedDataClass ]) {
303+ [ self writeByte: FlutterStandardFieldString] ;
304+ [ self writeUTF8: string] ;
305+ } else if ([value isKindOfClass: [FlutterStandardTypedData class ] ]) {
316306 FlutterStandardTypedData* typedData = value;
317- WriteByte (data, FlutterStandardFieldForDataType (typedData.type )) ;
318- WriteSize (data, typedData.elementCount ) ;
319- WriteAlignment (data, typedData.elementSize ) ;
320- WriteData (data, typedData.data ) ;
321- } else if ([value isKindOfClass: kNSDataClass ]) {
322- WriteValue (data, [FlutterStandardTypedData typedDataWithBytes: value]) ;
323- } else if ([value isKindOfClass: kNSArrayClass ]) {
307+ [ self writeByte: FlutterStandardFieldForDataType (typedData.type)] ;
308+ [ self writeSize: typedData.elementCount] ;
309+ [ self writeAlignment: typedData.elementSize] ;
310+ [ self writeData: typedData.data] ;
311+ } else if ([value isKindOfClass: [ NSData class ] ]) {
312+ [ self writeValue: [FlutterStandardTypedData typedDataWithBytes: value]] ;
313+ } else if ([value isKindOfClass: [ NSArray class ] ]) {
324314 NSArray * array = value;
325- WriteByte (data, FlutterStandardFieldList) ;
326- WriteSize (data, array.count ) ;
315+ [ self writeByte: FlutterStandardFieldList] ;
316+ [ self writeSize: array.count] ;
327317 for (id object in array) {
328- WriteValue (data, object) ;
318+ [ self writeValue: object] ;
329319 }
330- } else if ([value isKindOfClass: kNSDictionaryClass ]) {
320+ } else if ([value isKindOfClass: [ NSDictionary class ] ]) {
331321 NSDictionary * dict = value;
332- WriteByte (data, FlutterStandardFieldMap) ;
333- WriteSize (data, dict.count ) ;
322+ [ self writeByte: FlutterStandardFieldMap] ;
323+ [ self writeSize: dict.count] ;
334324 for (id key in dict) {
335- WriteValue (data, key) ;
336- WriteValue (data, [dict objectForKey: key]) ;
325+ [ self writeValue: key] ;
326+ [ self writeValue: [dict objectForKey: key]] ;
337327 }
338328 } else {
339329 NSLog (@" Unsupported value: %@ of type %@ " , value, [value class ]);
340- NSCAssert (NO , @" Unsupported value for standard codec. " );
330+ NSAssert (NO , @" Unsupported value for standard codec" );
341331 }
342332}
343-
344- - (void )writeByte : (UInt8)value {
345- WriteByte ((__bridge CFMutableDataRef)_data, value);
346- }
347-
348- - (void )writeBytes : (const void *)bytes length : (NSUInteger )length {
349- WriteBytes ((__bridge CFMutableDataRef)_data, bytes, length);
350- }
351-
352- - (void )writeData : (NSData *)data {
353- WriteData ((__bridge CFMutableDataRef)_data, data);
354- }
355-
356- - (void )writeSize : (UInt32)size {
357- WriteSize ((__bridge CFMutableDataRef)_data, size);
358- }
359-
360- - (void )writeAlignment : (UInt8)alignment {
361- WriteAlignment ((__bridge CFMutableDataRef)_data, alignment);
362- }
363-
364- - (void )writeUTF8 : (NSString *)value {
365- WriteUTF8 ((__bridge CFMutableDataRef)_data, value);
366- }
367-
368- - (void )writeValue : (id )value {
369- WriteValue ((__bridge CFMutableDataRef)_data, value);
370- }
371333@end
372334
373335@implementation FlutterStandardReader {
@@ -488,7 +450,7 @@ - (nullable id)readValueOfType:(UInt8)type {
488450 NSMutableArray * array = [NSMutableArray arrayWithCapacity: length];
489451 for (UInt32 i = 0 ; i < length; i++) {
490452 id value = [self readValue ];
491- [array addObject: (value == nil ? kNSNull : value)];
453+ [array addObject: (value == nil ? [ NSNull null ] : value)];
492454 }
493455 return array;
494456 }
@@ -498,7 +460,8 @@ - (nullable id)readValueOfType:(UInt8)type {
498460 for (UInt32 i = 0 ; i < size; i++) {
499461 id key = [self readValue ];
500462 id val = [self readValue ];
501- [dict setObject: (val == nil ? kNSNull : val) forKey: (key == nil ? kNSNull : key)];
463+ [dict setObject: (val == nil ? [NSNull null ] : val)
464+ forKey: (key == nil ? [NSNull null ] : key)];
502465 }
503466 return dict;
504467 }
0 commit comments