Package org.rypt.f8

Class Utf8


  • public class Utf8
    extends Object
    The core UTF-8 state machine.
    Author:
    Hans Brende (hansbrende@apache.org)
    • Method Detail

      • nextState

        @Deprecated
        public static int nextState​(int s,
                                    byte b)
        Deprecated.
        Returns the next UTF-8 state given a previous state and a next byte. If the returned state is non-negative, it is a legal non-surrogate unicode code point. Otherwise, it is either an error state or an incomplete state.
        Parameters:
        s - the previous UTF-8 state returned from this function, or 0 for the initial state
        b - the next byte
        Returns:
        the next UTF-8 state
        See Also:
        isErrorState(int), isIncompleteState(int)
      • nextState

        public static <X extends Exception> int nextState​(int s,
                                                          byte b,
                                                          Utf8ByteHandler<X> handler)
                                                   throws X extends Exception
        Returns the next UTF-8 state given a previous state, a next byte, and a code point handler.
        Parameters:
        s - the previous UTF-8 state returned from this function, or 0 for the initial state
        b - the next byte
        handler - the handler to delegate all code point and error handling to
        Returns:
        the next UTF-8 state
        Throws:
        X extends Exception
        See Also:
        isIncompleteState(int)
      • finish

        public static <X extends Exception> void finish​(int finalState,
                                                        Utf8ByteHandler<X> handler)
                                                 throws X extends Exception
        If the final state is incomplete, allows handler to handle final error state
        Parameters:
        finalState - the final state
        handler - the handler
        Throws:
        X extends Exception
      • transfer

        public static <X extends Exception> void transfer​(InputStream is,
                                                          Utf8ByteHandler<X> handler)
                                                   throws IOException,
                                                          X extends Exception
        This method is semantically equivalent to:
        
         int finalState = Utf8.nextState(0, is, handler);
         Utf8.finish(finalState, handler);
         
        Type Parameters:
        X - the handler exception type
        Parameters:
        is - the input stream
        handler - the handler
        Throws:
        IOException - if the input stream threw this exception
        X - if the handler threw this exception
        X extends Exception
      • validity

        public static Validity validity​(byte[] b,
                                        int from,
                                        int to)
        Returns the validity of the specified byte array between the specified indexes
        Parameters:
        b - the byte array
        from - the start index
        to - the end index, exclusive
        Returns:
        the validity
      • codePoint

        public static int codePoint​(int b1,
                                    int b2)
        Returns a valid unicode code point given a valid UTF-8 2-byte sequence. The maximum code point returned by this method is U+07FF. Results for invalid 2-byte sequences are undefined.
        Parameters:
        b1 - a negative byte of the form (byte)0b110xxxxx
        b2 - a negative byte of the form (byte)0b10xxxxxx
        Returns:
        the code point corresponding to this valid UTF-8 2-byte sequence
      • codePoint

        public static int codePoint​(int b1,
                                    int b2,
                                    int b3)
        Returns a valid unicode code point given a valid UTF-8 3-byte sequence. The maximum code point returned by this method is U+FFFF. Results for invalid 3-byte sequences are undefined.
        Parameters:
        b1 - a negative byte of the form (byte)0b1110xxxx
        b2 - a negative byte of the form (byte)0b10xxxxxx
        b3 - a negative byte of the form (byte)0b10xxxxxx
        Returns:
        the code point corresponding to this valid UTF-8 3-byte sequence
      • codePoint

        public static int codePoint​(int b1,
                                    int b2,
                                    int b3,
                                    int b4)
        Returns a valid unicode code point given a valid UTF-8 4-byte sequence. The maximum code point returned by this method is U+10FFFF. Results for invalid 4-byte sequences are undefined.
        Parameters:
        b1 - a negative byte of the form (byte)0b11110xxx
        b2 - a negative byte of the form (byte)0b10xxxxxx
        b3 - a negative byte of the form (byte)0b10xxxxxx
        b4 - a negative byte of the form (byte)0b10xxxxxx
        Returns:
        the code point corresponding to this valid UTF-8 4-byte sequence
      • isIncompleteState

        public static boolean isIncompleteState​(int s)
        Tests if at least one more continuation byte is needed to create a code point
        Parameters:
        s - the state to test
        Returns:
        true if at least one more byte is needed to create a code point
      • isErrorState

        public static boolean isErrorState​(int s)
        Tests if this state corresponds to any invalid UTF-8 byte sequence.
        Parameters:
        s - the state to test
        Returns:
        true if this state corresponds to an invalid UTF-8 byte sequence.
      • isSurrogatePrefixErrorState

        public static boolean isSurrogatePrefixErrorState​(int s)
        Tests if this state corresponds to an invalid UTF-8 2-byte sequence that prefixes a surrogate code point, i.e., in the range 0xED 0xA0 to 0xED 0xBF.
        Parameters:
        s - the state to test
        Returns:
        true if this state corresponds to an invalid surrogate code point prefix