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

Stringstring(ByteBuffer buffer)
Returns a string in the buffer.
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
return new String(bytes, StandardCharsets.UTF_8);
Stringstring(ByteBuffer buffer)
Decode a String representation.
return string(buffer, UTF_8);
Stringstring(ByteBuffer buffer, int position, int length, Charset charset)
Decode a String representation.
ByteBuffer copy = buffer.duplicate();
copy.position(position);
copy.limit(copy.position() + length);
return string(copy, charset);
Stringstring(ByteBuffer bytes)
string
if (bytes == null) {
    return null;
return string(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.remaining(), UTF8_ENCODING);
StringtoString(@Nonnull ByteBuffer buf, int len)
to String
byte[] b = new byte[Math.min(buf.remaining(), len)];
for (int i = 0; i < b.length; i++)
    b[i] = buf.get(buf.position() + i);
return "[" + Arrays.toString(b) + "...(" + buf.remaining() + " bytes)]";
StringtoString(ByteBuffer b, String separator)
to String
StringBuilder s = new StringBuilder();
for (int i = b.position(); i < b.limit(); i++) {
    if (i > b.position())
        s.append(separator);
    byte c = b.get(i);
    if (c >= 0)
        s.append(c);
    else
...
StringtoString(ByteBuffer bb)
to String
StringBuilder sb = new StringBuilder();
while (bb.hasRemaining()) {
    sb.append((char) bb.get());
return sb.toString();
StringtoString(ByteBuffer buf)
to String
StringBuilder sb = new StringBuilder();
StringBuilder hb = new StringBuilder();
byte b;
buf.mark();
while (buf.hasRemaining()) {
    b = buf.get();
    hb.append(String.format("%02X ", b));
    sb.append(String.format("%c", (char) b));
...
StringtoString(ByteBuffer buf, int len)
Converts the first len bytes from ByteBuffer buf into a String.
byte[] bytes = new byte[len];
buf.get(bytes);
return new String(bytes);
StringtoString(ByteBuffer buffer)
to String
byte bytes[] = new byte[buffer.remaining()];
buffer.get(bytes);
return new String(bytes);