Java UTF8 Encode encodeUTF8(String str)

Here you can find the source of encodeUTF8(String str)

Description

Encode a string as UTF-8.

License

Open Source License

Parameter

Parameter Description
str the string to encode

Return

the UTF-8 representation of str

Declaration

public static byte[] encodeUTF8(String str) 

Method Source Code

//package com.java2s;
/*//from   ww w.  j ava 2 s  .  c om
 * Copyright (c) 2004, PostgreSQL Global Development Group
 * See the LICENSE file in the project root for more information.
 */

import java.nio.charset.Charset;

public class Main {
    /**
     * Keep a local copy of the UTF-8 Charset so we can avoid synchronization overhead from looking up
     * the Charset by name as String.getBytes(String) requires.
     */
    private final static Charset utf8Charset = Charset.forName("UTF-8");

    /**
     * Encode a string as UTF-8.
     *
     * @param str the string to encode
     * @return the UTF-8 representation of {@code str}
     */
    public static byte[] encodeUTF8(String str) {
        // See org.postgresql.benchmark.encoding.UTF8Encoding#string_getBytes
        // for performance measurements.
        // In OracleJDK 6u65, 7u55, and 8u40 String.getBytes(Charset) is
        // 3 times faster than other JDK approaches.
        return str.getBytes(utf8Charset);
    }
}

Related

  1. encodeSignatureUtf8(byte[] sig)
  2. encodeStringUtf8(String text)
  3. encodeToUTF8(String str)
  4. encodeUtf8(byte[] bytes)
  5. encodeUTF8(byte[] bytes)
  6. encodeUTF8(String str)
  7. encodeUtf8(String string)
  8. encodeUtf8(String target)
  9. encodeUTF8(String text)