Java Utililty Methods Byte Array to String by Charset

List of utility methods to do Byte Array to String by Charset

Description

The list of methods to do Byte Array to String by Charset are organized into topic(s).

Method

StringtoString(byte[] data, int from, int length, Charset charset)
Convert to string with UTF8 charset.
return new String(Arrays.copyOfRange(data, from, from + length), charset);
StringtoString(final byte[] bytes, final String charsetName)
Converts a byte[] to a String using the specified character encoding.
return charsetName != null ? new String(bytes, charsetName) : new String(bytes, Charset.defaultCharset());
StringtoUnicode(byte[] isoStr, String charset)
to Unicode
return new String(isoStr, Charset.forName(charset));
StringtruncateByByteLength(String string, int maxBytes, Charset charset)
truncate By Byte Length
CharsetEncoder ce = charset.newEncoder();
if (string.length() * ce.maxBytesPerChar() <= maxBytes)
    return string;
CharBuffer chars = CharBuffer.wrap(string);
ByteBuffer bytes = ByteBuffer.allocate(maxBytes);
CoderResult result = ce.encode(chars, bytes, true);
return result.isOverflow() ? new String(bytes.array(), 0, bytes.position(), charset) : string;
Listyaml2List(Charset charset, byte[] data)
yaml List
List<String> list = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(data), charset));
String line = null;
while ((line = reader.readLine()) != null) {
    String[] kvs = line.split(" ");
    if (kvs.length == 2) {
        list.add(kvs[1].trim());
return list;