A way to convert a ByteBuffer to a String would be to use a Charset to decode the bytes:
Charset charset = Charset.forName("ISO-8859-1");
ByteBuffer m_buffer = ...;
String text = charset.decode(m_buffer).toString();
The decoding creates a CharBuffer
which you can conveniently convert to a String
. You can reuse the CharSet
and it is threadsafe. I wouldn't worry too much about performance (re "fastest way") unless you have really a problem in that area. A general advice, when you want to use ByteBuffer, do the to-String conversion as late as possible, and only when the String is needed.
As 14jbella mentioned, Strings are immutable. There is no way of creating a String from an array (char or byte) that does not include copying the data because arrays are mutable. So no, there is no way to do it without copying.
Further you should take into consideration, that m_buffer.array()
returns the internal array of the ByteBuffer
, which may be much more than the actual data stored in the buffer. Creating a String from that array might lead to a potentially huge memory allocation, because the data gets copied into a new array. For example if you're using a 256 MB ByteBuffer somewhere in your code, and you get a slice()
of 32 bytes named m_buffer
from that buffer to convert to a String, your invocation of new String(m_buffer.array())
would allocate a new byte array of the size of the original, backing byte array, which is 256 MB, which is probably not that fast, if it requires a GC.
Btw. new String(byte[])
internally uses a CharSet
decoder on a ByteBuffer
wrapped around your input byte array.