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

byte[]getBytesUnchecked(String string, String charsetName)
get Bytes Unchecked
if (string == null) {
    return null;
} else {
    try {
        return string.getBytes(charsetName);
    } catch (UnsupportedEncodingException var3) {
        throw newIllegalStateException(charsetName, var3);
char[]getChars(byte[] data, String charset)
get Chars
return getChars(data, 0, data != null ? data.length : 0, charset);
StringgetContentAsString(byte[] content, Charset charset)
Converts the byte array into a String based on the specified charset.
if (charset == null) {
    throw new IllegalArgumentException("Charset cannot be null");
return new String(content, charset);
byte[]getMaxBytes(String string, int maxBytes, Charset charSet)
Returns the first maxBytes of string encoded using the encoder of charSet
byte[] bytes = new byte[maxBytes];
ByteBuffer out = ByteBuffer.wrap(bytes);
CharBuffer in = CharBuffer.wrap(string.toCharArray());
CharsetEncoder encoder = charSet.newEncoder();
CoderResult cr = encoder.encode(in, out, true);
encoder.flush(out);
if (cr.isError()) {
    cr.throwException();
...
intgetNumberOfBytesPerCharacter(final String charsetName)
Returns the number of bytes per character in the given charset.
if (!isFixedWith(charsetName)) {
    return -1;
final Charset charset = Charset.forName(charsetName);
final CharsetEncoder encoder = charset.newEncoder();
return (int) encoder.maxBytesPerChar();
StringgetPrefixWithMaxBytes(String string, int maxBytes, Charset charSet)
Returns the prefix of string which takes up a maximum of maxBytes.
try {
    return new String(getMaxBytes(string, maxBytes, charSet), charSet.name());
} catch (UnsupportedEncodingException uee) {
    throw new RuntimeException("Could not recreate string", uee);
StringgetRowId(final byte[] rowId, final Charset charset, final boolean base64encodeValues)
get Row Id
if (base64encodeValues) {
    ByteBuffer cellRowBuffer = ByteBuffer.wrap(rowId);
    ByteBuffer base64Buffer = Base64.getEncoder().encode(cellRowBuffer);
    return new String(base64Buffer.array(), StandardCharsets.UTF_8);
} else {
    return new String(rowId, charset);
StringgetString(@Nonnull byte[] b, @Nonnegative int bufferSize, @Nonnegative int offset, @Nonnull Charset charset)
get String
final int chunkSize = getStringChunkSizeFor(bufferSize);
checkBufferEnd(b, offset, chunkSize);
final int byteCount = getInteger(b, offset);
final String result;
if (byteCount >= 0) {
    result = new String(b, offset + INTEGER_CHUNK_SIZE, byteCount, charset);
} else {
    result = null;
...
StringgetString(byte[] bytes, String charset)
get String
try {
    return new String(bytes, charset);
} catch (UnsupportedEncodingException e) {
    throw new IllegalArgumentException(e);
StringgetString(byte[] bytesIn, Charset cs)
get String
return getString(bytesIn, cs, false);