Class StringEncoder


  • public final class StringEncoder
    extends Object
    Encodes Strings to bytes.
    Since:
    2.5
    • Method Detail

      • toBytes

        public static byte[] toBytes​(String str,
                                     Charset charset)
        Converts a String to a byte[].
        Parameters:
        str - if null, return null.
        charset - if null, use the default charset.
        Returns:
        a byte[]
      • encodeSingleByteChars

        @Deprecated
        public static byte[] encodeSingleByteChars​(CharSequence s)
        Deprecated.
        No longer necessary given better performance in Java 8
        Prefer standard String.getBytes(Charset) which performs better in Java 8 and beyond. Encodes the specified char sequence by casting each character to a byte.
        Parameters:
        s - the char sequence to encode
        Returns:
        the encoded String
        See Also:
        LOG4J2-1151
      • encodeIsoChars

        @Deprecated
        public static int encodeIsoChars​(CharSequence charArray,
                                         int charIndex,
                                         byte[] byteArray,
                                         int byteIndex,
                                         int length)
        Deprecated.
        No longer necessary given better performance in Java 8
        Prefer standard String.getBytes(Charset) which performs better in Java 8 and beyond. Implementation note: this is the fast path. If the char array contains only ISO-8859-1 characters, all the work will be done here.
      • encodeString

        @Deprecated
        public static int encodeString​(CharSequence charArray,
                                       int charOffset,
                                       int charLength,
                                       byte[] byteArray)
        Deprecated.
        No longer necessary given better performance in Java 8
        Prefer standard String.getBytes(Charset) which performs better in Java 8 and beyond.