code: public static int calculateEncodedStringLength(String value) {
          int encodedLen = 1;
          if (value != null) {
              int valueLength = value.length();
              for (int i = 0; i < valueLength; i++) {
                  int c = value.charAt(i);
                  if (c <= (0x7f - 2)) {
                      encodedLen++;
                  } else if (c <= (12415 - 2)) {
                      encodedLen += 2;
                  } else {
                      if (c >= 0xd800 && c <= 0xdbff) {
                          // Found a high surrogate. Verify that surrogate pair is
                          // well-formed. Low surrogate must follow high surrogate.
                          if (i + 1 < valueLength) {
                              int c2 = value.charAt(i + 1);
                              if (c2 >= 0xdc00 && c2 <= 0xdfff) {
                                  i++;
                              }
                          }
                      }
                      encodedLen += 3;
                  }
              }
          }
          return encodedLen;
      }
docs: Returns the amount of bytes required to encode the given String.
  
  @param value String to encode, may be null