Java UTF8 utf8ByteLength(String string)

Here you can find the source of utf8ByteLength(String string)

Description

Calculates the byte length of a UTF-8 encoded string.

License

Open Source License

Parameter

Parameter Description
string string to be computed

Return

byte length of a UTF-8 string in bytes, 0 if null.

Declaration

public static long utf8ByteLength(String string) 

Method Source Code

//package com.java2s;
/**// www. j  av a 2  s . com
 * Copyright 2013-2016 Amazon.com, 
 * Inc. or its affiliates. All Rights Reserved.
 * 
 * Licensed under the Amazon Software License (the "License"). 
 * You may not use this file except in compliance with the 
 * License. A copy of the License is located at
 * 
 *     http://aws.amazon.com/asl/
 * 
 * or in the "license" file accompanying this file. This file is 
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, express or implied. See the License 
 * for the specific language governing permissions and 
 * limitations under the License.
 */

import java.nio.charset.Charset;

public class Main {
    /**
     * UTF-8 {@link Charset}
     */
    private static final Charset UTF_8 = Charset.forName("UTF-8");

    /**
     * Calculates the byte length of a UTF-8 encoded string. 0 if the string is
     * null.
     * 
     * @param string string to be computed
     * @return byte length of a UTF-8 string in bytes, 0 if null.
     */
    public static long utf8ByteLength(String string) {
        if (string == null) {
            return 0;
        }
        return string.getBytes(UTF_8).length;
    }
}

Related

  1. toBytesUTF8(String s)
  2. toBytesUTF8(String str)
  3. unZip(String zipFile, String outputFolder, boolean skipDirectory)
  4. utf8(String string)
  5. utf8BufferByteLen(CharSequence str)
  6. utf8BytesToString(byte[] bytes, int start, int length)
  7. utf8BytesToString(final byte[] in_utf8Bytes)
  8. utf8CharLen(byte byte1)
  9. UTF8GetBytes(final String value)