Flutter Engine
FlutterStandardWriter Class Reference

#import <FlutterCodecs.h>

Inheritance diagram for FlutterStandardWriter:

Instance Methods

(instancetype) - initWithData:
 
(void) - writeByte:
 
(void) - writeBytes:length:
 
(void) - writeData:
 
(void) - writeSize:
 
(void) - writeAlignment:
 
(void) - writeUTF8:
 
(void) - writeValue:
 

Detailed Description

A writer of the Flutter standard binary encoding.

See FlutterStandardMessageCodec for details on the encoding.

The encoding is extensible via subclasses overriding writeValue.

Definition at line 92 of file FlutterCodecs.h.

Method Documentation

◆ initWithData:()

- (instancetype) initWithData: (NSMutableData*)  data
Initial value:
{
NSMutableData* _data

Create a FlutterStandardWriter who will write to data.

Definition at line 212 of file FlutterStandardCodec.mm.

212  :(NSMutableData*)data {
213  self = [super init];
214  NSAssert(self, @"Super init cannot be nil");
215  _data = [data retain];
216  return self;
217 }

◆ writeAlignment:()

- (void) writeAlignment: (UInt8)  alignment

Write zero padding until data is aligned with alignment.

Definition at line 249 of file FlutterStandardCodec.mm.

References writeByte:.

Referenced by writeValue:.

249  :(UInt8)alignment {
250  UInt8 mod = _data.length % alignment;
251  if (mod) {
252  for (int i = 0; i < (alignment - mod); i++) {
253  [self writeByte:0];
254  }
255  }
256 }

◆ writeByte:()

- (void) writeByte: (UInt8)  value

Write a 8-bit byte.

Definition at line 224 of file FlutterStandardCodec.mm.

Referenced by FlutterStandardMethodCodec::codecWithReaderWriter:, writeAlignment:, writeSize:, and writeValue:.

224  :(UInt8)value {
225  [_data appendBytes:&value length:1];
226 }
uint8_t value

◆ writeBytes:length:()

- (void) writeBytes: (const void*)  bytes
length: (NSUInteger)  length 

Write an array of bytes of size length.

Definition at line 228 of file FlutterStandardCodec.mm.

Referenced by writeSize:, writeUTF8:, and writeValue:.

228  :(const void*)bytes length:(NSUInteger)length {
229  [_data appendBytes:bytes length:length];
230 }
size_t length

◆ writeData:()

- (void) writeData: (NSData*)  data

Write an array of bytes contained in data.

Definition at line 232 of file FlutterStandardCodec.mm.

Referenced by writeValue:.

232  :(NSData*)data {
233  [_data appendData:data];
234 }

◆ writeSize:()

- (void) writeSize: (UInt32)  size

Write 32-bit unsigned integer that represents a size of a collection.

Definition at line 236 of file FlutterStandardCodec.mm.

References fml::size(), value, writeByte:, and writeBytes:length:.

Referenced by writeUTF8:, and writeValue:.

236  :(UInt32)size {
237  if (size < 254) {
238  [self writeByte:(UInt8)size];
239  } else if (size <= 0xffff) {
240  [self writeByte:254];
241  UInt16 value = (UInt16)size;
242  [self writeBytes:&value length:2];
243  } else {
244  [self writeByte:255];
245  [self writeBytes:&size length:4];
246  }
247 }
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
uint8_t value

◆ writeUTF8:()

- (void) writeUTF8: (NSString*)  value

Write a string with UTF-8 encoding.

Definition at line 258 of file FlutterStandardCodec.mm.

References length, writeBytes:length:, and writeSize:.

Referenced by writeValue:.

258  :(NSString*)value {
259  UInt32 length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
260  [self writeSize:length];
261  [self writeBytes:value.UTF8String length:length];
262 }
uint8_t value
size_t length

◆ writeValue:()

- (void) writeValue: (id value

Introspects into an object and writes its representation.

Supported Data Types:

  • NSNull
  • NSNumber
  • NSString (as UTF-8)
  • FlutterStandardTypedData
  • NSArray of supported types
  • NSDictionary of supporte types

NSAsserts on failure.

Definition at line 264 of file FlutterStandardCodec.mm.

References FlutterStandardTypedData::typedDataWithBytes:, value, writeAlignment:, writeByte:, writeBytes:length:, writeData:, writeSize:, writeUTF8:, and writeValue:.

Referenced by FlutterStandardMethodCodec::codecWithReaderWriter:, FlutterStandardMessageCodec::codecWithReaderWriter:, and writeValue:.

264  :(id)value {
265  if (value == nil || value == [NSNull null]) {
266  [self writeByte:FlutterStandardFieldNil];
267  } else if ([value isKindOfClass:[NSNumber class]]) {
268  CFNumberRef number = (CFNumberRef)value;
269  BOOL success = NO;
270  if (CFGetTypeID(number) == CFBooleanGetTypeID()) {
271  BOOL b = CFBooleanGetValue((CFBooleanRef)number);
272  [self writeByte:(b ? FlutterStandardFieldTrue : FlutterStandardFieldFalse)];
273  success = YES;
274  } else if (CFNumberIsFloatType(number)) {
275  Float64 f;
276  success = CFNumberGetValue(number, kCFNumberFloat64Type, &f);
277  if (success) {
278  [self writeByte:FlutterStandardFieldFloat64];
279  [self writeAlignment:8];
280  [self writeBytes:(UInt8*)&f length:8];
281  }
282  } else if (CFNumberGetByteSize(number) <= 4) {
283  SInt32 n;
284  success = CFNumberGetValue(number, kCFNumberSInt32Type, &n);
285  if (success) {
286  [self writeByte:FlutterStandardFieldInt32];
287  [self writeBytes:(UInt8*)&n length:4];
288  }
289  } else if (CFNumberGetByteSize(number) <= 8) {
290  SInt64 n;
291  success = CFNumberGetValue(number, kCFNumberSInt64Type, &n);
292  if (success) {
293  [self writeByte:FlutterStandardFieldInt64];
294  [self writeBytes:(UInt8*)&n length:8];
295  }
296  }
297  if (!success) {
298  NSLog(@"Unsupported value: %@ of number type %ld", value, CFNumberGetType(number));
299  NSAssert(NO, @"Unsupported value for standard codec");
300  }
301  } else if ([value isKindOfClass:[NSString class]]) {
302  NSString* string = value;
303  [self writeByte:FlutterStandardFieldString];
304  [self writeUTF8:string];
305  } else if ([value isKindOfClass:[FlutterStandardTypedData class]]) {
306  FlutterStandardTypedData* typedData = value;
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]]) {
314  NSArray* array = value;
315  [self writeByte:FlutterStandardFieldList];
316  [self writeSize:array.count];
317  for (id object in array) {
318  [self writeValue:object];
319  }
320  } else if ([value isKindOfClass:[NSDictionary class]]) {
321  NSDictionary* dict = value;
322  [self writeByte:FlutterStandardFieldMap];
323  [self writeSize:dict.count];
324  for (id key in dict) {
325  [self writeValue:key];
326  [self writeValue:[dict objectForKey:key]];
327  }
328  } else {
329  NSLog(@"Unsupported value: %@ of type %@", value, [value class]);
330  NSAssert(NO, @"Unsupported value for standard codec");
331  }
332 }
uint8_t value
int32_t id

The documentation for this class was generated from the following files: