writeStringUtf8 method

void writeStringUtf8(
  1. int id,
  2. Uint8List utf8, [
  3. int? length
])

Writes a string field from its UTF-8 bytes — the first length of utf8, all of them when omitted — so an InlineString's storage and length go out as they are, uncopied and untranscoded.

The bytes are validated first (CORELIB_PLAN §6.4): a string the encoder emits is valid UTF-8 whichever way it was handed over, and invalid bytes are refused with SofabError.invalidArgument before anything is written.

Implementation

void writeStringUtf8(int id, Uint8List utf8, [int? length]) {
  final n = _countOf(length, utf8.length);
  if (!utf8Valid(utf8, 0, n)) {
    throw const SofabException(
      SofabError.invalidArgument,
      'string bytes are not valid UTF-8',
    );
  }
  _writeHeaderAndVarint(id, WireType.fixlen, (n << 3) | FixlenType.string);
  _writeRaw(utf8, 0, n);
}