Description
I am trying to migrate Krossbow, my STOMP-over-websocket multiplatform library, and I stumbled upon this function becoming private/internal in kotlinx-io
.
I relied on it because I need to truncate a string based on its length in UTF-8 bytes (not characters). The use case is the "close reason" (the text payload) of the web socket Close
frame. As per the specification:
All control frames MUST have a payload length of 125 bytes or less and MUST NOT be fragmented.
With the utf8Size
function, I could optimize the happy path by not copying bytes around if the string was short enough. Without this function, I need to encode the string into a byte array uncondtionally, then check if it's too long, in which case I need to truncate.
I think there are probably more use cases where this is useful information to have without actually copying the bytes.