Java Utililty Methods ByteBuffer to String

List of utility methods to do ByteBuffer to String

Description

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

Method

StringtoString(ByteBuffer buffer)
Convert the buffer to an ISO-8859-1 String
return toString(buffer, StandardCharsets.ISO_8859_1);
StringtoString(ByteBuffer buffer)
to String
StringBuffer response = new StringBuffer();
Charset charset = Charset.forName("UTF-8");
response.append(charset.decode(buffer));
buffer.flip();
return response.toString();
StringtoString(ByteBuffer buffer)
to String
StringBuffer response = new StringBuffer();
Charset charset = Charset.forName("UTF-8");
response.append(charset.decode(buffer));
buffer.flip();
return response.toString();
StringtoString(ByteBuffer buffer)
Converts the contents of the specified byte buffer to a string, which is formatted similarly to the output of the Arrays#toString() method.
StringBuilder builder = new StringBuilder("[");
for (int i = 0; i < buffer.limit(); i++) {
    String hex = Integer.toHexString(buffer.get(i) & 0xFF).toUpperCase();
    if (hex.length() == 1)
        hex = "0" + hex;
    builder.append("0x").append(hex);
    if (i != buffer.limit() - 1) {
        builder.append(", ");
...
StringtoString(ByteBuffer buffer)
to String
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
return new String(bytes, "utf-8");
StringtoString(ByteBuffer buffer)
Convert the ByteBuffer to a UTF-8 String.
if (buffer == null)
    return null;
byte[] array = buffer.hasArray() ? buffer.array() : null;
if (array == null) {
    byte[] to = new byte[buffer.remaining()];
    buffer.slice().get(to);
    return new String(to, 0, to.length, StandardCharsets.UTF_8);
return new String(array, buffer.arrayOffset() + buffer.position(), buffer.remaining(),
        StandardCharsets.UTF_8);
StringtoString(ByteBuffer buffer, Charset charset)
to String
buffer = buffer.slice();
byte[] buf = new byte[buffer.remaining()];
buffer.get(buf);
return new String(buf, charset);
StringtoString(ByteBuffer buffer, int offset, int length)
Constructs a new String by decoding the specified subarray of bytes using the ASCII charset.
if (offset < 0 || length < 0 || offset + length > buffer.limit()) {
    throw new IndexOutOfBoundsException();
int position = buffer.position();
int limit = buffer.limit();
buffer.position(offset);
buffer.limit(offset + length);
String result = StandardCharsets.US_ASCII.decode(buffer).toString();
...
StringtoString(ByteBuffer buffer, String encoding)
to String
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
return fromBytes(bytes, encoding);
StringtoString(ByteBuffer bytes)
Parses a string out of a byte buffer whose content is UTF-8 encoded.
return toString(toByteArray(bytes));