Java Utililty Methods UTF8

List of utility methods to do UTF8

Description

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

Method

byte[]getUtf8Bytes(String s)
Convinient method to get UTF8 bytes
return s.getBytes(Charset.forName("UTF-8"));
byte[]GetUtf8Bytes(String str, boolean replace)
Encodes a string in UTF-8 as a byte array.
return GetUtf8Bytes(str, replace, false);
byte[]getUTF8Bytes(String string)
Converts the string to bytes using the UTF-8 encoding.
try {
    return string.getBytes(UTF_8_CHARSET.name());
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);
byte[]getUTF8BytesFromString(String str)
get UTF Bytes From String
return str.getBytes(UTF_8);
CharsetDecodergetUtf8Decoder()
get Utf Decoder
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
return decoder;
CharsetgetUtf8OrDefault()
Returns the value of #getUtf8() if UTF8 is supported.
if (Charset.isSupported(UTF8_NAME)) {
    return getUtf8();
} else {
    return Charset.defaultCharset();
ReadergetUTF8Reader(InputStream f)
get UTF Reader
BufferedInputStream bis = new BufferedInputStream(f);
assert bis.markSupported();
bis.mark(3);
boolean reset = true;
byte[] t = new byte[3];
bis.read(t);
if (t[0] == ((byte) 0xef) && t[1] == ((byte) 0xbb) && t[2] == ((byte) 0xbf)) {
    reset = false;
...
StringgetUtf8String(URL url)
get Utf String
try {
    return Resources.toString(url, UTF_8);
} catch (IOException ex) {
    throw new UndeclaredThrowableException(ex);
PrintStreamgetUTF8SuportOutput()
get UTF Suport Output
PrintStream out = new PrintStream(System.out, true, "UTF-8");
return out;
WritergetUTF8Writer(String path)
Returns a Writer for writing UTF-8 encoded data to the file at the specified path.
FileOutputStream out = new FileOutputStream(path);
OutputStreamWriter writer = null;
try {
    writer = new OutputStreamWriter(out, _UTF8_ENCODING);
} catch (UnsupportedEncodingException e) {
    assert false;
    return null;
return new BufferedWriter(writer);