The UTF-8 encoding uses sequences of 1, 2, or 3 bytes per character. With
he maximal length of the fragment we want to ensure, that there are no
overflow of 65536 byte sized buffer

@param str
String to be written in the output stream
@throws IOException
4529
public void writeFragmentedUTF(String str) throws IOException {
		if (str.length() <= MAX_LENGTH) {
			writeLastUTFFragment(str);
		} else {
			writeUTFFragment(str.substring(0, MAX_LENGTH));
			writeFragmentedUTF(str.substring(MAX_LENGTH));
		}
	}
791431
public static byte[] toUtf8(String str) {
        int i, max;
        char c;
        int len;
        byte[] result;

        max = str.length();
        len = 0;
        for (i = 0; i < max; i++) {
            c = str.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007f)) {
                len += 1;
            } else {
                if ((c == 0) || ((c >= 0x0080) && (c <= 0x07ff))) {
                    len += 2;
                } else {
                    len += 3;
                }
            }
        }
        if (len > 0xffff) {
            throw new IllegalArgumentException("string too long: " + str.length() + "/" + len);
        }
        result = new byte[1 + 2 + len];
        result[0] = CONSTANT_UTF8;
        result[1] = (byte) (len >> 8);
        result[2] = (byte) len;
        len = 3;  // used for current position
        for (i = 0; i < max; i++) {
            c = str.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007f)) {
                result[len] = (byte) c;
                len += 1;
            } else {
                if ((c == 0) || ((c >= 0x0080) && (c <= 0x07ff))) {
                    result[len + 0] = (byte) ((c >> 6) | 0xc0);
                    result[len + 1] = (byte) ((c & 0x3f) | 0x80);
                    len += 2;
                } else {
                    result[len + 0] = (byte) ((c >> 12) | 0xe0);
                    result[len + 1] = (byte) (((c >> 6) & 0x3f) | 0x80);
                    result[len + 2] = (byte) ((c & 0x3f) | 0x80);
                    len += 3;
                }
            }
        }
        if (len != result.length) {
            throw new IllegalStateException(len + " vs " + result.length);
        }
        return result;
    }
320381
public static String encode(String str)
    {
        String encoded;
        try {
            encoded = URLEncoder.encode(str, "UTF-8").replace(".", "%2E").replace("*", "%2A");
        } catch (UnsupportedEncodingException e) {
            // Should never happen

            encoded = str;
        }

        return encoded;
    }
824345
public static String decode(String str) {
		try {
			return URLDecoder.decode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			//should never happen
			throw new RuntimeException(e);
		}
	}
1123286
protected void writeString(OutputStream out, String str) throws IOException {
        if (str == null) {
            writeInt(out, -1);
        } else if (str.isEmpty()) {
            writeInt(out, 0);
        } else {
            byte[] bs = StringSerializer.encode(str);
            writeInt(out, bs.length);
            out.write(bs);
        }
    }
452294
private static final byte[] getSimpleBytes(String str) {
        StringBuilder sb = new StringBuilder(str);
        byte[] b = new byte[sb.length()];
        for (int i = 0, len = sb.length(); i < len; i++) {
            b[i] = (byte) sb.charAt(i);
        }
        return b;
    }
1068582
public static int getDBLength(String str, int n) {
		if (str == null) {
			return 0;
		}

		char[] chars = str.toCharArray();
		int len = 0;

		for (int i = 0; i < chars.length; i++) {
			if (Character.UnicodeBlock.of(chars[i]) == Character.UnicodeBlock.BASIC_LATIN) {
				len++;
			} else {
				len += n;
			}
		}

		return len;
	}
1033568
public static int writeString(DataOutput out, String s) throws IOException {
    if (s.length() > 0xffff/3) {         // maybe too long
      LOG.warn("truncating long string: " + s.length()
               + " chars, starting with " + s.substring(0, 20));
      s = s.substring(0, 0xffff/3);
    }
    
    int len = utf8Length(s);
    if (len > 0xffff)                             // double-check length
      throw new IOException("string too long!");
      
    out.writeShort(len);
    writeChars(out, s, 0, s.length());
    return len;
  }
1231864
void writeExtensionString(int extensionType, String str) throws IOException {
        byte[] bytes = str.getBytes(UTF_8);
        if (bytes.length > 256) {
            throw new IllegalArgumentException("String too long");
        }
        output.write(EXT_8);
        output.write(bytes.length);
        output.write(extensionType);
        output.write(bytes);
    }
1099009
private static void writeString(String str, DataOutput writer) throws IOException {
		if (str == null) {
			writer.writeByte(0);
		} else {
			int blocks = str.length() >> 15;
			// Use smallest number of bytes possible for number of blocks.
			if (blocks > 0x3FFFFF) {
				// Need 4 bytes
				writer.writeByte(0xc0 | (blocks >> 24));
				writer.writeByte(0xFF & (blocks >> 16));
				writer.writeByte(0xFF & (blocks >> 8));
			} else if (blocks > 0x3FFF) {
				// Need 3 bytes
				writer.writeByte(0x80 | (blocks >> 16));
				writer.writeByte(0xFF & (blocks >> 8));
			} else if (blocks > 0x3F) {
				// Need 2 bytes
				writer.writeByte(0x40 | (blocks >> 8));
			}
			// Need to add 1 to number of blocks to avoid confusion with 'null'
			writer.writeByte(0xFF & (blocks+1));
			int end = 0;
			for (int i=0; i<blocks; i++) {
				int start = end;
				end = (i+1)<<15;
				writer.writeUTF(str.substring(start, end));
			}
			writer.writeUTF(str.substring(end));
		}
	}
1118000
public void writeUnmodifiedUTF (String str)
        throws IOException
    {
        // byte[] bytes = str.getBytes(Charsets.UTF_8); // TODO Java 6 (Charsets is from guava)
        byte[] bytes = str.getBytes("UTF-8");
        writeShort(bytes.length);
        write(bytes);
    }
1071739
public static CharStream of(final CharSequence str) {
        return N.isNullOrEmpty(str) ? empty() : of(str, 0, str.length());
    }
1292539
public static String decode(String str) {
    if (str != null) {
      try {
        return URLDecoder.decode(str, UTF_8.name());
      } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("JVM must support UTF-8", e);
      }
    }
    return null;
  }
1242014
public static String encode(final String str) {
        try {
            return URLEncoder.encode(str, "UTF-8");
        } catch (final Exception e) {
            LOGGER.log(Level.WARN, "Encodes str [" + str + "] failed", e);

            return str;
        }
    }
1049504
public static UTF8String fromString(String str) {
    return str == null ? null : fromBytes(str.getBytes(StandardCharsets.UTF_8));
  }
529105
function utf8Encode(str) {
  var bytes = [], offset = 0, length, char;

  str = encodeURI(str);
  length = str.length;

  while (offset < length) {
    char = str.charAt(offset);
    offset += 1;

    if (char !== '%') {
      bytes.push(char.charCodeAt(0));
    } else {
      char = str.charAt(offset) + str.charAt(offset + 1);
      bytes.push(parseInt(char, 16));
      offset += 2;
    }
  }

  return bytes;
}
1090178
@Override
	public void writeString(String str) throws IOException {
		byte[] bytes = Utf8.getBytesFor(str);
		writeBytes(bytes, 0, bytes.length);
	}