Java UTF8 utf8StringSizeInBytes(String s)

Here you can find the source of utf8StringSizeInBytes(String s)

Description

Compute the size in bytes of the given string encoded as utf8.

License

Open Source License

Declaration

public static int utf8StringSizeInBytes(String s) 

Method Source Code

//package com.java2s;
// Use of this source code is governed by a BSD-style license that can be

public class Main {
    /**//from www .java  2 s . c o m
     * Compute the size in bytes of the given string encoded as utf8.
     */
    public static int utf8StringSizeInBytes(String s) {
        int res = 0;
        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            int codepoint = c;
            if (isSurrogate(c)) {
                i++;
                char c2 = s.charAt(i);
                codepoint = Character.toCodePoint(c, c2);
            }
            res += 1;
            if (codepoint > 0x7f) {
                res += 1;
                if (codepoint > 0x7ff) {
                    res += 1;
                    if (codepoint > 0xffff) {
                        res += 1;
                        if (codepoint > 0x1fffff) {
                            res += 1;
                            if (codepoint > 0x3ffffff) {
                                res += 1;
                            }
                        }
                    }
                }
            }
        }
        return res;
    }

    /**
     * Determines if the given {@code char} value is a Unicode <i>surrogate code unit</i>. See
     * {@link Character#isSurrogate}. Extracting here because the method only exists at API level
     * 19.
     */
    private static boolean isSurrogate(char c) {
        return c >= Character.MIN_SURROGATE
                && c < (Character.MAX_SURROGATE + 1);
    }
}

Related

  1. utf8Length(byte[] buffer, int str, int len)
  2. utf8Replace(String str)
  3. utf8SafeCEscape(String src)
  4. utf8String(String... strs)
  5. utf8StringLength(final CharSequence sequence)
  6. utf8StringToByteArray(String dataString)
  7. utf8StringToBytes(String string)
  8. utf8ToString(byte[] data)
  9. utf8URLDecode(String input)