Class Utf8


  • public final class Utf8
    extends Object
    Low-level, high-performance utility methods related to the UTF-8 character encoding. UTF-8 is defined in section D92 of The Unicode Standard Core Specification, Chapter 3.

    The variant of UTF-8 implemented by this class is the restricted definition of UTF-8 introduced in Unicode 3.1. One implication of this is that it rejects "non-shortest form" byte sequences, even though the JDK decoder may accept them.

    Since:
    16.0
    Author:
    Martin Buchholz, Clément Roux
    • Constructor Detail

      • Utf8

        public Utf8()
    • Method Detail

      • encodedLength

        public static int encodedLength​(CharSequence sequence)
        Returns the number of bytes in the UTF-8-encoded form of sequence. For a string, this method is equivalent to string.getBytes(UTF_8).length, but is more efficient in both time and space.
        Throws:
        IllegalArgumentException - if sequence contains ill-formed UTF-16 (unpaired surrogates)
      • encodedLengthGeneral

        private static int encodedLengthGeneral​(CharSequence sequence,
                                                int start)
      • unpairedSurrogateMsg

        private static String unpairedSurrogateMsg​(int i)