Java Utililty Methods UTF8 Encode

List of utility methods to do UTF8 Encode

Description

The list of methods to do UTF8 Encode are organized into topic(s).

Method

byte[]utf8Encode(String s)
This method is required to work around a stupid bug in Sun's JDK String.getBytes("UTF-8") (bug #4628881).
final char[] chars = s.toCharArray();
int elen;
elen = 0;
for (int i = 0; i < s.length(); i++) {
    int ch = chars[i];
    if (ch >= 0x0001 && ch <= 0x007f)
        elen++;
    else if (ch == 0x0000 || (ch >= 0x0080 && ch <= 0x07ff))
...
Stringutf8Encode(String str)
utf Encode
if (!isEmpty(str) && str.getBytes().length != str.length()) {
    try {
        return URLEncoder.encode(str, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
return str;
byte[]utf8Encode(String str)
utf Encode
return str.getBytes(StandardCharsets.UTF_8);
StringUtf8Encode(String string)
Utf Encode
string = string.replace("\\r\\n", "\n");
String utftext = "";
for (int n = 0; n < string.length(); n++) {
    char c = string.charAt(n);
    if (c < 128) {
        utftext += c;
    } else if ((c > 127) && (c < 2048)) {
        utftext += ((c >> 6) | 192);
...
Stringutf8Encode(String url)
utf Encode
return urlEncode(url, StandardCharsets.UTF_8);
byte[]utf8FromString(String sIn)
Converts a String to a block of utf8 bytes
final String DEFAULT_CHARSET = "UTF-8";
byte bufReturn[] = null;
try {
    bufReturn = sIn.getBytes(DEFAULT_CHARSET);
} catch (Exception e) {
return bufReturn;
Stringutf8urlencode(String text)
utfurlencode
StringBuilder result = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
    char c = text.charAt(i);
    if (c <= 255) {
        result.append(c);
    } else {
        byte[] b = new byte[0];
        try {
...