writeUTF8 method

void writeUTF8(
  1. String s
)

Write the given string as an unsigned short giving the byte length, followed by that many bytes containing the UTF8 encoding. The short is written using the current endian format. This should be compatible with Java's DataOutputStream.writeUTF8(), as long as the string contains no nulls and no UTF formats beyond the 1, 2 and 3 byte formats. See https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/DataInput.html#modified-utf-8

Throws an ArgumentError if the encoded string length doesn't fit in an unsigned short.

Implementation

void writeUTF8(String s) {
  final utf8 = (const Utf8Encoder()).convert(s);
  if (utf8.length > 0xffffffff) {
    throw ArgumentError(
        'Byte length ${utf8.length} exceeds maximum of ${0xffffffff}');
  }
  writeUnsignedShort(utf8.length);
  _dest.add(utf8);
}