encodeWriteFileContent function

Uint8List encodeWriteFileContent(
  1. Object content
)

Normalizes content (String or List<int>) into raw bytes for BaseEnvironment.writeFile.

Throws a FormatException when a String contains lone UTF-16 surrogates, or an ArgumentError when content is neither a String nor a List<int>.

Implementation

Uint8List encodeWriteFileContent(Object content) {
  if (content is Uint8List) {
    return content;
  }
  if (content is List<int>) {
    return Uint8List.fromList(content);
  }
  if (content is String) {
    if (hasLoneUtf16Surrogates(content)) {
      throw const FormatException(
        'Cannot UTF-8 encode string containing lone UTF-16 surrogates.',
      );
    }
    return Uint8List.fromList(utf8.encode(content));
  }
  throw ArgumentError.value(
    content,
    'content',
    'content must be String or List<int>, got ${content.runtimeType}',
  );
}