Utf8Safe

class Utf8Safe : Utf8

A set of low-level, high-performance static utility methods related to the UTF-8 character encoding. This class has no dependencies outside of the core JDK libraries.

There are several variants of UTF-8. The one implemented by this class is the restricted definition of UTF-8 introduced in Unicode 3.1, which mandates the rejection of "overlong" byte sequences as well as rejection of 3-byte surrogate codepoint byte sequences. Note that the UTF-8 decoder included in Oracle's JDK has been modified to also reject "overlong" byte sequences, but (as of 2011) still accepts 3-byte surrogate codepoint byte sequences.

The byte sequences considered valid by this class are exactly those that can be roundtrip converted to Strings and back to bytes using the UTF-8 charset, without loss:


Arrays.equals(bytes, new String(bytes, Internal.UTF_8).getBytes(Internal.UTF_8))

See the Unicode Standard, Table 3-6. UTF-8 Bit Distribution, Table 3-7. Well Formed UTF-8 Byte Sequences.

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
open fun decodeUtf8(buffer: ByteBuffer, offset: Int, length: Int): String
Decodes the given UTF-8 portion of the ByteBuffer into a String.
Link copied to clipboard
Returns the number of bytes in the UTF-8-encoded form of sequence.
Link copied to clipboard
open fun encodeUtf8(in: CharSequence, out: ByteBuffer)
Encodes the given characters to the target ByteBuffer using UTF-8 encoding.
Link copied to clipboard
open fun getDefault(): Utf8
Get the default UTF-8 processor.
Link copied to clipboard
open fun setDefault(instance: Utf8)
Set the default instance of the UTF-8 processor.