writeUtf8 method

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

Writes an UTF-8 string. Returns number of written bytes.

Implementation

int writeUtf8(String value, {int maxLengthInBytes}) {
  // Write until the first multi-byte rune.
  final multiByteRuneIndex = _writeUtf8Simple(value);
  if (multiByteRuneIndex<0) {
    // All runes were single-byte.
    return value.length;
  }

  // Remove written prefix.
  value = value.substring(multiByteRuneIndex);

  // Write the rest using UTF8-encoder
  final utf8Bytes = utf8.encode(value);
  if (maxLengthInBytes != null && utf8Bytes.length >= maxLengthInBytes) {
    throw new ArgumentError.value(
      value,
      "value",
      "string exceeds maximum length ($maxLengthInBytes) when encoded to UTF-8",
    );
  }
  writeBytes(utf8Bytes);
  return utf8Bytes.length;
}