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

StringconvertBufferToString(ByteBuffer buffer)
convert Buffer To String
if (buffer.remaining() == 0)
    return "";
byte[] bytes = new byte[buffer.limit()];
buffer.get(bytes);
return new String(bytes);
StringdecodeString(ByteBuffer bb)
decode String
return Charset.forName("UTF-8").decode(bb).toString();
StringdecodeString(ByteBuffer buffer, String charset)
Convert the bytes in a given buffer to a string, using a given charset.
if (buffer == null) {
    return null;
if (charset == null) {
    throw new IllegalArgumentException("charset");
try {
    return new String(buffer.array(), buffer.position(), buffer.limit() - buffer.position(), charset);
...
StringdecodeString(ByteBuffer src)
decode String
CharsetDecoder theDecoder = decoder.get();
theDecoder.reset();
final CharBuffer dst = CharBuffer.allocate((int) ((double) src.remaining() * theDecoder.maxCharsPerByte()));
CoderResult cr = theDecoder.decode(src, dst, true);
if (!cr.isUnderflow())
    cr.throwException();
cr = theDecoder.flush(dst);
if (!cr.isUnderflow())
...
StringgetStr(ByteBuffer buff)
read a String from a ByteBuffer that was written w/the putStr method
short len = buff.getShort();
if (len == 0) {
    return null;
} else {
    byte[] b = new byte[len];
    buff.get(b);
    return new String(b);
StringgetString(@Nonnull final ByteBuffer src)
get String
final int size = src.getInt();
if (size == -1) {
    return null;
final char[] array = new char[size];
for (int i = 0; i < size; i++) {
    array[i] = src.getChar();
return new String(array);
StringgetString(ByteBuffer bb)
Inputs a string from a ByteBuffer.
int size = bb.getShort();
char[] charBuffer = new char[size];
for (int i = 0; i < size; i++) {
    charBuffer[i] = bb.getChar();
return new String(charBuffer, 0, size);
StringgetString(ByteBuffer buf)
Gets an RS2 string from the buffer.
StringBuilder bldr = new StringBuilder();
char c;
while ((c = (char) buf.get()) != 10) {
    bldr.append(c);
return bldr.toString();
StringgetString(ByteBuffer buf)
get String
int len = buf.getShort(); 
if (len == -1)
    return null;
char[] cs = new char[len];
for (int i = 0; i < len; i++) {
    cs[i] = buf.getChar();
return new String(cs);
...
StringgetString(ByteBuffer buf)
get String
StringBuilder bldr = new StringBuilder();
int b;
while ((b = (buf.get() & 0xFF)) != 0) {
    bldr.append((char) b);
return bldr.toString();