code: private static int offsetOf65KUtf8Bytes(String str, int startIndex, int endIndex) {
      // This implementation is based off of Utf8.encodedLength
      int utf8Length = 0;
      int i = startIndex;
      for (; i < endIndex; i++) {
        char c = str.charAt(i);
        utf8Length++;
        if (c < 0x800) {
          utf8Length += (0x7f - c) >>> 31; // branch free!
        } else {
          utf8Length += Character.isSurrogate(c) ? 1 : 2;
        }
        if (utf8Length == MAX_CONSTANT_STRING_LENGTH) {
          return i + 1;
        } else if (utf8Length > MAX_CONSTANT_STRING_LENGTH) {
          return i;
        }
      }
      return endIndex;
    }
docs: Returns the largest index between {@code startIndex} and {@code endIdex} such that the UTF8
  encoded bytes of {@code str.substring(startIndex, returnValue}} is less than or equal to 65K.