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[]decodePayloadFromUtf8(String utf8string)
Helper method that takes an UTF-8 string and returns its byte representation.
return utf8string.getBytes(UTF8_CHARSET);
StringdecodeUTF8(byte[] bytes)
decode UTF
try {
    return new String(bytes, UTF_8.name());
} catch (UnsupportedEncodingException e) {
    throw new AssertionError(e);
StringdecodeUtf8(String url)
decode Utf
try {
    return URLDecoder.decode(url, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException("Unable to encode using charset");
StringdecUTF8(String s)
dec UTF
try {
    return URLDecoder.decode(s, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
    throw new IllegalStateException("Standard encoding UTF-8 is missing");
voiddumpStringArray(Path outputFilePath, String[] stringArray)
dump String Array
try (BufferedWriter os = Files.newBufferedWriter(outputFilePath, Charset.forName("UTF-8"))) {
    for (int i = 0; i < stringArray.length; i++) {
        os.write(stringArray[i]);
        os.newLine();
voiddumpUtf8ToFile(List records, String delimiter, File file)
Dump UTF-8 records to the given file using the specified delimiter (or newline if null).
byte[] delimiterBytes = (delimiter != null ? delimiter : "\n").getBytes(StandardCharsets.UTF_8);
byte[] d = new byte[] {};
try (FileOutputStream output = new FileOutputStream(file)) {
    for (String line : records) {
        output.write(d); 
        d = delimiterBytes;
        output.write(line.getBytes(StandardCharsets.UTF_8));
StringfromUTF8(byte[] ba)
from UTF
return new String(ba, StandardCharsets.UTF_8);
StringfromUTF8(byte[] bytes)
from UTF
return new String(bytes, UTF8);
StringfromUTF8(byte[] bytes)
Decodes UTF-8 bytes to String.
return fromUTF8(bytes, 0, bytes.length);
byte[]generateRawUTF8Bytes(final String string)
Generates a byte array that is populated with the UTF-8 encoding of the provided String.
return string.getBytes(StandardCharsets.UTF_8);