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[]toUTF8Bytes(final String s)
to UTF Bytes
try {
    return s.getBytes("UTF-8");
} catch (final UnsupportedEncodingException e) {
    throw new RuntimeException(e);
byte[]toUTF8Bytes(String src)
to UTF Bytes
if (src == null) {
    return null;
byte[] bytes = null;
try {
    bytes = src.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
return bytes;
byte[]toUTF8Bytes(String string)
to UTF Bytes
return toBytes(string, CHARSET_UTF_8);
InputStreamtoUTF8InputStream(String str)
Replacement for deprecated StringBufferInputStream().
InputStream is = null;
try {
    is = new ByteArrayInputStream(str.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
    throw new AssertionError();
return is;
StringtoUTF8String(byte[] b, int offset, int length)
to UTF String
try {
    return new String(b, offset, length, __UTF8);
} catch (UnsupportedEncodingException e) {
    throw new IllegalArgumentException(e);
Stringutf8Code(String str)
Get the literal presentation of utf8 string
StringBuilder ostr = new StringBuilder();
String hex;
for (int i = 0; i < str.length(); i++) {
    char ch = str.charAt(i);
    if ((ch >= 0x0020) && (ch <= 0x007e)) {
        ostr.append(ch);
    } else {
        ostr.append("\\u"); 
...
booleanUtf8codeCheck(String text)
Utfcode Check
String sign = "";
if (text.startsWith("%e")) {
    for (int p = 0; p != -1;) {
        p = text.indexOf("%", p);
        if (p != -1) {
            p++;
        sign += p;
...
Stringutf8Encode(final Collection col)
Encodes the given collection of strings using HttpUtil#ENCODING encoding.
final StringBuilder param = new StringBuilder();
for (final String elem : col) {
    param.append(elem).append(",");
return utf8Encode(param.substring(0, param.length() - 1));
Stringutf8Encode(final String s)
UTF-8 encode the string.
if (s == null) {
    return null;
try {
    return URLEncoder.encode(s, UTF_8);
} catch (UnsupportedEncodingException e) {
    return s;
byte[]utf8Encode(final String value)
This will convert the supplied value to a UTF-8 encoded byte array.
return value != null ? value.getBytes(UTF8_CHARSET) : null;