Class JavaEncoder


  • class JavaEncoder
    extends Encoder
    JavaEncoder -- Encoder for Java based strings. Useful if in Java code generators to generate efficiently encoded strings for arbitrary data. This encoder uses the minimal sequence of characters required to encode a character (e.g. standard backslash escapes, such as "\n", "\\" , "\'", octal escapes, and unicode escapes). This encoder does NOT check UTF-16 surrogate pair sequences. The target output context supports mismatched UTF-16 pairs (e.g. it will compile, run, etc... with them).
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) static int OCT_ESCAPE_LENGTH
      The length of a octal escape sequence, e.g.
      (package private) static int OCT_MASK
      The bit-mask for an octal unit.
      (package private) static int OCT_SHIFT
      Number of bits to shift for each octal unit.
      (package private) static int U_ESCAPE_LENGTH
      The length of a Unicode escape, e.g.
    • Constructor Summary

      Constructors 
      Constructor Description
      JavaEncoder()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.nio.charset.CoderResult encodeArrays​(java.nio.CharBuffer input, java.nio.CharBuffer output, boolean endOfInput)
      The core encoding loop used when both the input and output buffers are array backed.
      protected int firstEncodedOffset​(java.lang.String input, int off, int len)
      Scans the input string for the first character index that requires encoding.
      protected int maxEncodedLength​(int n)
      Returns the maximum encoded length (in chars) of an input sequence of n characters.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • U_ESCAPE_LENGTH

        static final int U_ESCAPE_LENGTH
        The length of a Unicode escape, e.g. "\\u1234".
        See Also:
        Constant Field Values
      • OCT_ESCAPE_LENGTH

        static final int OCT_ESCAPE_LENGTH
        The length of a octal escape sequence, e.g. "\377".
        See Also:
        Constant Field Values
      • OCT_SHIFT

        static final int OCT_SHIFT
        Number of bits to shift for each octal unit.
        See Also:
        Constant Field Values
    • Constructor Detail

      • JavaEncoder

        JavaEncoder()
    • Method Detail

      • maxEncodedLength

        protected int maxEncodedLength​(int n)
        Description copied from class: Encoder
        Returns the maximum encoded length (in chars) of an input sequence of n characters.
        Specified by:
        maxEncodedLength in class Encoder
        Parameters:
        n - the number of characters of input
        Returns:
        the worst-case number of characters required to encode
      • firstEncodedOffset

        protected int firstEncodedOffset​(java.lang.String input,
                                         int off,
                                         int len)
        Description copied from class: Encoder
        Scans the input string for the first character index that requires encoding. If the entire input does not require encoding then the length is returned. This method is used by the Encode.forXYZ methods to return input strings unchanged when possible.
        Specified by:
        firstEncodedOffset in class Encoder
        Parameters:
        input - the input to check for encoding
        off - the offset of the first character to check
        len - the number of characters to check
        Returns:
        the index of the first character to encode. The return value will be off+len if no characters in the input require encoding.
      • encodeArrays

        protected java.nio.charset.CoderResult encodeArrays​(java.nio.CharBuffer input,
                                                            java.nio.CharBuffer output,
                                                            boolean endOfInput)
        Description copied from class: Encoder
        The core encoding loop used when both the input and output buffers are array backed. The loop is expected to fetch the arrays and interact with the arrays directly for performance.
        Overrides:
        encodeArrays in class Encoder
        Parameters:
        input - the input buffer.
        output - the output buffer.
        endOfInput - when true, this is the last input to encode
        Returns:
        UNDERFLOW or OVERFLOW