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 220 of file FlutterStandardCodec.mm.

220  :(NSMutableData*)data {
221  self = [super init];
222  NSAssert(self, @"Super init cannot be nil");
223  _data = [data retain];
224  return self;
225 }
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition: switches.h:41

◆ writeAlignment:()

- (void) writeAlignment: (UInt8)  alignment

Write zero padding until data is aligned with alignment.

Definition at line 257 of file FlutterStandardCodec.mm.

References writeByte:.

Referenced by writeValue:.

257  :(UInt8)alignment {
258  UInt8 mod = _data.length % alignment;
259  if (mod) {
260  for (int i = 0; i < (alignment - mod); i++) {
261  [self writeByte:0];
262  }
263  }
264 }

◆ writeByte:()

- (void) writeByte: (UInt8)  value

Write a 8-bit byte.

Definition at line 232 of file FlutterStandardCodec.mm.

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

232  :(UInt8)value {
233  [_data appendBytes:&value length:1];
234 }
uint8_t value

◆ writeBytes:length:()

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

Write an array of bytes of size length.

Definition at line 236 of file FlutterStandardCodec.mm.

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

236  :(const void*)bytes length:(NSUInteger)length {
237  [_data appendBytes:bytes length:length];
238 }
size_t length

◆ writeData:()

- (void) writeData: (NSData*)  data

Write an array of bytes contained in data.

Definition at line 240 of file FlutterStandardCodec.mm.

Referenced by writeValue:.

240  :(NSData*)data {
241  [_data appendData:data];
242 }
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition: switches.h:41

◆ writeSize:()

- (void) writeSize: (UInt32)  size

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

Definition at line 244 of file FlutterStandardCodec.mm.

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

Referenced by writeUTF8:, and writeValue:.

244  :(UInt32)size {
245  if (size < 254) {
246  [self writeByte:(UInt8)size];
247  } else if (size <= 0xffff) {
248  [self writeByte:254];
249  UInt16 value = (UInt16)size;
250  [self writeBytes:&value length:2];
251  } else {
252  [self writeByte:255];
253  [self writeBytes:&size length:4];
254  }
255 }
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 266 of file FlutterStandardCodec.mm.

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

Referenced by writeValue:.

266  :(NSString*)value {
267  UInt32 length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
268  [self writeSize:length];
269  [self writeBytes:value.UTF8String length:length];
270 }
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 272 of file FlutterStandardCodec.mm.

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

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

272  :(id)value {
273  if (value == nil || value == [NSNull null]) {
274  [self writeByte:FlutterStandardFieldNil];
275  } else if ([value isKindOfClass:[NSNumber class]]) {
276  CFNumberRef number = (CFNumberRef)value;
277  BOOL success = NO;
278  if (CFGetTypeID(number) == CFBooleanGetTypeID()) {
279  BOOL b = CFBooleanGetValue((CFBooleanRef)number);
280  [self writeByte:(b ? FlutterStandardFieldTrue : FlutterStandardFieldFalse)];
281  success = YES;
282  } else if (CFNumberIsFloatType(number)) {
283  Float64 f;
284  success = CFNumberGetValue(number, kCFNumberFloat64Type, &f);
285  if (success) {
286  [self writeByte:FlutterStandardFieldFloat64];
287  [self writeAlignment:8];
288  [self writeBytes:(UInt8*)&f length:8];
289  }
290  } else if (CFNumberGetByteSize(number) <= 4) {
291  SInt32 n;
292  success = CFNumberGetValue(number, kCFNumberSInt32Type, &n);
293  if (success) {
294  [self writeByte:FlutterStandardFieldInt32];
295  [self writeBytes:(UInt8*)&n length:4];
296  }
297  } else if (CFNumberGetByteSize(number) <= 8) {
298  SInt64 n;
299  success = CFNumberGetValue(number, kCFNumberSInt64Type, &n);
300  if (success) {
301  [self writeByte:FlutterStandardFieldInt64];
302  [self writeBytes:(UInt8*)&n length:8];
303  }
304  }
305  if (!success) {
306  NSLog(@"Unsupported value: %@ of number type %ld", value, CFNumberGetType(number));
307  NSAssert(NO, @"Unsupported value for standard codec");
308  }
309  } else if ([value isKindOfClass:[NSString class]]) {
310  NSString* string = value;
311  [self writeByte:FlutterStandardFieldString];
312  [self writeUTF8:string];
313  } else if ([value isKindOfClass:[FlutterStandardTypedData class]]) {
314  FlutterStandardTypedData* typedData = value;
315  [self writeByte:FlutterStandardFieldForDataType(typedData.type)];
316  [self writeSize:typedData.elementCount];
317  [self writeAlignment:typedData.elementSize];
318  [self writeData:typedData.data];
319  } else if ([value isKindOfClass:[NSData class]]) {
320  [self writeValue:[FlutterStandardTypedData typedDataWithBytes:value]];
321  } else if ([value isKindOfClass:[NSArray class]]) {
322  NSArray* array = value;
323  [self writeByte:FlutterStandardFieldList];
324  [self writeSize:array.count];
325  for (id object in array) {
326  [self writeValue:object];
327  }
328  } else if ([value isKindOfClass:[NSDictionary class]]) {
329  NSDictionary* dict = value;
330  [self writeByte:FlutterStandardFieldMap];
331  [self writeSize:dict.count];
332  for (id key in dict) {
333  [self writeValue:key];
334  [self writeValue:[dict objectForKey:key]];
335  }
336  } else {
337  NSLog(@"Unsupported value: %@ of type %@", value, [value class]);
338  NSAssert(NO, @"Unsupported value for standard codec");
339  }
340 }
uint8_t value
int BOOL
Definition: windows_types.h:37
int32_t id

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