Here you can find the source of utf8StringSizeInBytes(String s)
public static int utf8StringSizeInBytes(String s)
//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); } }