in trace/src/main/org/jetbrains/lincheck/trace/Streams.kt [230:259]
override fun writeUTF(s: String) {
val strlen = s.length
var utflen = strlen // optimized for ASCII
repeat(strlen) { i ->
val c = s[i].code
if (c >= 0x80 || c == 0) utflen += if (c >= 0x800) 2 else 1
}
require(utflen < 65536 && utflen >= strlen) { "Invalid input string" }
if (buffer.remaining() < 2 + utflen) {
throw BufferOverflowException()
}
buffer.putShort(utflen.toShort())
repeat(strlen) { i ->
val c: Int = s[i].code
if (c < 0x80 && c != 0) {
buffer.put(c.toByte())
} else if (c >= 0x800) {
buffer.put((0xE0 or ((c shr 12) and 0x0F)).toByte())
buffer.put((0x80 or ((c shr 6) and 0x3F)).toByte())
buffer.put((0x80 or ((c shr 0) and 0x3F)).toByte())
} else {
buffer.put((0xC0 or ((c shr 6) and 0x1F)).toByte())
buffer.put((0x80 or ((c shr 0) and 0x3F)).toByte())
}
}
}