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 bytes)
Extract the byte[] within the given ByteBuffer and decode into a String using UTF-8.
return toString(copyBytes(bytes));
StringtoString(ByteBuffer sequence)
Convert a byte buffer to a string in platform default encoding.
return new String(sequence.array(), sequence.position(), sequence.remaining());
StringtoString(ByteBuffer value, String charsetName)
convert a ByteBuffer to a String ByteBuffer is reset to its original state.
String result = null;
try {
    result = new String(toBytes(value), charsetName);
} catch (Exception e) {
return result;
StringtoString(final ByteBuffer buffer)
Returns the buffer as a string while preserving the buffer position and limit.
final int position = buffer.position();
final int limit = buffer.limit();
final byte[] data = new byte[buffer.remaining()];
buffer.get(data);
final String dataString = new String(data);
buffer.position(position);
buffer.limit(limit);
return dataString;
...
StringtoString(final ByteBuffer buffer)
Convert ByteBuffer to string, assuming UTF-8 encoding in the buffer.
byte[] array = new byte[buffer.remaining()];
buffer.get(array);
return new String(array, Charset.forName("UTF-8"));
StringtoString(final ByteBuffer buffer)
Converts a byte buffer into a string in the UTF-8 character set.
return toCharBuffer(buffer).toString();
StringtoStringBinary(ByteBuffer buf)
Converts the given byte buffer, from its array offset to its limit, to a string.
if (buf == null)
    return "null";
return toStringBinary(buf.array(), buf.arrayOffset(), buf.limit());
StringtoStringBinary(ByteBuffer buf)
Converts the given byte buffer, from its array offset to its limit, to a string.
if (buf == null)
    return "null";
return toStringBinary(buf.array(), buf.arrayOffset(), buf.limit());
voidtoText(ByteBuffer data, StringBuilder result, int cnt)
Gets last cnt read bytes from the data buffer and puts into result buffer in special format:
  • if byte represents char from partition 0x1F to 0x80 (which are normal ascii chars) then it's put into buffer as it is
  • otherwise dot is put into buffer
int charPos = data.position() - cnt;
for (int a = 0; a < cnt; a++) {
    int c = data.get(charPos++);
    if (c > 0x1f && c < 0x80)
        result.append((char) c);
    else
        result.append('.');
StringtoTextAll(ByteBuffer bytes)
Convert whole byte buffer to UTF-8 String.
return new String(bytes.array(), UTF8);