Class ByteUtils


  • public class ByteUtils
    extends Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_UTF8_BYTES_PER_CHAR
      Maximum number of UTF8 bytes per UTF16 character.
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteUtils()  
    • Field Detail

      • MAX_UTF8_BYTES_PER_CHAR

        public static final int MAX_UTF8_BYTES_PER_CHAR
        Maximum number of UTF8 bytes per UTF16 character.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ByteUtils

        public ByteUtils()
    • Method Detail

      • UTF8toUTF16

        public static int UTF8toUTF16​(byte[] utf8,
                                      int offset,
                                      int len,
                                      char[] out,
                                      int out_offset)
        Converts utf8 to utf16 and returns the number of 16 bit Java chars written. Full characters are read, even if this reads past the length passed (and can result in an ArrayOutOfBoundsException if invalid UTF8 is passed). Explicit checks for valid UTF8 are not performed. The char[] out should probably have enough room to hold the worst case of each byte becoming a Java char.
      • UTF8toUTF16

        public static void UTF8toUTF16​(byte[] utf8,
                                       int offset,
                                       int len,
                                       org.noggit.CharArr out)
        Convert UTF8 bytes into UTF16 characters.
      • UTF8toUTF16

        public static String UTF8toUTF16​(byte[] utf8,
                                         int offset,
                                         int len)
        Convert UTF8 bytes into a String
      • UTF16toUTF8

        public static int UTF16toUTF8​(CharSequence s,
                                      int offset,
                                      int len,
                                      byte[] result,
                                      int resultOffset)
        Writes UTF8 into the byte array, starting at offset. The caller should ensure that there is enough space for the worst-case scenario.
        Returns:
        the number of bytes written
      • writeUTF16toUTF8

        public static int writeUTF16toUTF8​(CharSequence s,
                                           int offset,
                                           int len,
                                           OutputStream fos,
                                           byte[] scratch)
                                    throws IOException
        Writes UTF8 into the given OutputStream by first writing to the given scratch array and then writing the contents of the scratch array to the OutputStream. The given scratch byte array is used to buffer intermediate data before it is written to the output stream.
        Returns:
        the number of bytes written
        Throws:
        IOException
      • calcUTF16toUTF8Length

        public static int calcUTF16toUTF8Length​(CharSequence s,
                                                int offset,
                                                int len)
        Calculates the number of UTF8 bytes necessary to write a UTF16 string.
        Returns:
        the number of bytes written