writeUtf8 method

int writeUtf8(
  1. String value, {
  2. int? maxLengthInBytes,
})

Writes a UTF-8 string. Returns the number of bytes written.

Implementation

int writeUtf8(String value, {int? maxLengthInBytes}) {
  final multiByteRuneIndex = _writeUtf8Simple(value);
  if (multiByteRuneIndex < 0) return value.length;

  // Encode remaining multi-byte part
  final suffix = value.substring(multiByteRuneIndex);
  final utf8Bytes = utf8.encode(suffix);

  if (maxLengthInBytes != null && utf8Bytes.length >= maxLengthInBytes) {
    throw ArgumentError.value(
      value,
      "value",
      "string exceeds maximum length ($maxLengthInBytes) when encoded to UTF-8",
    );
  }
  writeBytes(utf8Bytes);
  return utf8Bytes.length;
}