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

StringasStr(ByteBuffer buff)
as Str
return new String(buff.array(), buff.arrayOffset() + buff.position(), buff.limit());
StringasString(ByteBuffer buffer, int length)
Returns the contents of a buffer as a string, converting ASCII characters in the buffer, into unicode string characters.
char[] chars = new char[length];
for (int i = 0; i < length; i++) {
    chars[i] = (char) buffer.get(i);
return String.valueOf(chars);
StringasString(final ByteBuffer buffer)
as String
final byte[] bytes = new byte[buffer.limit()];
buffer.get(bytes);
return new String(bytes, "US-ASCII");
StringasString(Map headers)
as String
if (headers == null) {
    return null;
Iterator<Entry<ByteBuffer, ByteBuffer>> i = headers.entrySet().iterator();
if (!i.hasNext()) {
    return "{}";
StringBuilder sb = new StringBuilder();
...
StringbbToString(ByteBuffer bb)
bb To String
return new String(bbToArray(bb));
Stringbuffer2String(ByteBuffer buff, String charsetName)
buffer String
return Charset.forName(charsetName).decode(buff).toString();
StringbufferToString(ByteBuffer buf)
buffer To String
try {
    boolean isString = true;
    byte[] arr = buf.array();
    for (int i = 0; i < buf.remaining(); i++) {
        byte c = arr[i];
        if (c <= 127 || ((c & 0xC0) == 0x80) || ((c & 0xE0) == 0xC0))
            continue;
        isString = false;
...
StringbufferToString(ByteBuffer buf)
buffer To String
return new String(buf.array(), StandardCharsets.UTF_8);
StringbufferToString(ByteBuffer buff)
buffer To String
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
String message = "";
try {
    message = decoder.decode(buff).toString();
} catch (Exception e) {
    message = e.getMessage();
return message;
StringbufferToString(ByteBuffer buffer)
buffer To String
if (buffer.hasArray()) {
    return new String(buffer.array(), buffer.arrayOffset(), buffer.remaining());
} else {
    int oldPos = buffer.position();
    byte[] bytes = new byte[buffer.remaining()];
    buffer.get(bytes);
    buffer.position(oldPos);
    return new String(bytes);
...