code: public static long countBytes(String s, boolean shortLength) throws UTFDataFormatException {
          long result = 0;
          final int length = s.length();
          for (int i = 0; i < length; ++i) {
              char ch = s.charAt(i);
              if (ch != 0 && ch <= 127) { // U+0000 uses two bytes.
                  ++result;
              } else if (ch <= 2047) {
                  result += 2;
              } else {
                  result += 3;
              }
              if (shortLength && result > 65535) {
                  throw new UTFDataFormatException("String more than 65535 UTF bytes long");
              }
          }
          return result;
      }
docs: Returns the number of bytes the modified UTF-8 representation of 's' would take. Note
  that this is just the space for the bytes representing the characters, not the length
  which precedes those bytes, because different callers represent the length differently,
  as two, four, or even eight bytes. If {@code shortLength} is true, we'll throw an
  exception if the string is too long for its length to be represented by a short.