6
6
7
7
#pragma mark - Codec for basic message channel
8
8
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
-
20
9
@implementation FlutterStandardMessageCodec {
21
10
FlutterStandardReaderWriter* _readerWriter;
22
11
}
@@ -232,142 +221,115 @@ - (void)dealloc {
232
221
[super dealloc ];
233
222
}
234
223
235
- static void WriteByte ( CFMutableDataRef data, UInt8 value) {
236
- CFDataAppendBytes (data, &value, 1 ) ;
224
+ - ( void ) writeByte : ( UInt8 ) value {
225
+ [_data appendBytes: &value length: 1 ] ;
237
226
}
238
227
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] ;
241
230
}
242
231
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] ;
245
234
}
246
235
247
- static void WriteSize ( CFMutableDataRef data, UInt32 size) {
236
+ - ( void ) writeSize : ( UInt32 ) size {
248
237
if (size < 254 ) {
249
- WriteByte (data, (UInt8 )size) ;
238
+ [ self writeByte: (UInt8 )size] ;
250
239
} else if (size <= 0xffff ) {
251
- WriteByte (data, 254 ) ;
240
+ [ self writeByte: 254 ] ;
252
241
UInt16 value = (UInt16 )size;
253
- WriteBytes (data, &value, 2 ) ;
242
+ [ self writeBytes: &value length: 2 ] ;
254
243
} else {
255
- WriteByte (data, 255 ) ;
256
- WriteBytes (data, &size, 4 ) ;
244
+ [ self writeByte: 255 ] ;
245
+ [ self writeBytes: &size length: 4 ] ;
257
246
}
258
247
}
259
248
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;
263
251
if (mod) {
264
- WriteBytes (data, kZeroBuffer , alignment - mod);
252
+ for (int i = 0 ; i < (alignment - mod); i++) {
253
+ [self writeByte: 0 ];
254
+ }
265
255
}
266
256
}
267
257
268
- static void WriteUTF8 ( CFMutableDataRef data, NSString * value) {
258
+ - ( void ) writeUTF8 : ( NSString *) value {
269
259
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] ;
272
262
}
273
263
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 ] ]) {
278
268
CFNumberRef number = (CFNumberRef )value;
279
269
BOOL success = NO ;
280
270
if (CFGetTypeID (number) == CFBooleanGetTypeID ()) {
281
271
BOOL b = CFBooleanGetValue ((CFBooleanRef )number);
282
- WriteByte (data, (b ? FlutterStandardFieldTrue : FlutterStandardFieldFalse)) ;
272
+ [ self writeByte: (b ? FlutterStandardFieldTrue : FlutterStandardFieldFalse)] ;
283
273
success = YES ;
284
274
} else if (CFNumberIsFloatType (number)) {
285
275
Float64 f;
286
276
success = CFNumberGetValue (number, kCFNumberFloat64Type , &f);
287
277
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 ] ;
291
281
}
292
282
} else if (CFNumberGetByteSize (number) <= 4 ) {
293
283
SInt32 n;
294
284
success = CFNumberGetValue (number, kCFNumberSInt32Type , &n);
295
285
if (success) {
296
- WriteByte (data, FlutterStandardFieldInt32) ;
297
- WriteBytes (data, (UInt8 *)&n, 4 ) ;
286
+ [ self writeByte: FlutterStandardFieldInt32] ;
287
+ [ self writeBytes: (UInt8 *)&n length: 4 ] ;
298
288
}
299
289
} else if (CFNumberGetByteSize (number) <= 8 ) {
300
290
SInt64 n;
301
291
success = CFNumberGetValue (number, kCFNumberSInt64Type , &n);
302
292
if (success) {
303
- WriteByte (data, FlutterStandardFieldInt64) ;
304
- WriteBytes (data, (UInt8 *)&n, 8 ) ;
293
+ [ self writeByte: FlutterStandardFieldInt64] ;
294
+ [ self writeBytes: (UInt8 *)&n length: 8 ] ;
305
295
}
306
296
}
307
297
if (!success) {
308
298
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" );
310
300
}
311
- } else if ([value isKindOfClass: kNSStringClass ]) {
301
+ } else if ([value isKindOfClass: [ NSString class ] ]) {
312
302
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 ] ]) {
316
306
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 ] ]) {
324
314
NSArray * array = value;
325
- WriteByte (data, FlutterStandardFieldList) ;
326
- WriteSize (data, array.count ) ;
315
+ [ self writeByte: FlutterStandardFieldList] ;
316
+ [ self writeSize: array.count] ;
327
317
for (id object in array) {
328
- WriteValue (data, object) ;
318
+ [ self writeValue: object] ;
329
319
}
330
- } else if ([value isKindOfClass: kNSDictionaryClass ]) {
320
+ } else if ([value isKindOfClass: [ NSDictionary class ] ]) {
331
321
NSDictionary * dict = value;
332
- WriteByte (data, FlutterStandardFieldMap) ;
333
- WriteSize (data, dict.count ) ;
322
+ [ self writeByte: FlutterStandardFieldMap] ;
323
+ [ self writeSize: dict.count] ;
334
324
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]] ;
337
327
}
338
328
} else {
339
329
NSLog (@" Unsupported value: %@ of type %@ " , value, [value class ]);
340
- NSCAssert (NO , @" Unsupported value for standard codec. " );
330
+ NSAssert (NO , @" Unsupported value for standard codec" );
341
331
}
342
332
}
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
- }
371
333
@end
372
334
373
335
@implementation FlutterStandardReader {
@@ -488,7 +450,7 @@ - (nullable id)readValueOfType:(UInt8)type {
488
450
NSMutableArray * array = [NSMutableArray arrayWithCapacity: length];
489
451
for (UInt32 i = 0 ; i < length; i++) {
490
452
id value = [self readValue ];
491
- [array addObject: (value == nil ? kNSNull : value)];
453
+ [array addObject: (value == nil ? [ NSNull null ] : value)];
492
454
}
493
455
return array;
494
456
}
@@ -498,7 +460,8 @@ - (nullable id)readValueOfType:(UInt8)type {
498
460
for (UInt32 i = 0 ; i < size; i++) {
499
461
id key = [self readValue ];
500
462
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)];
502
465
}
503
466
return dict;
504
467
}
0 commit comments