In addition to Adam Rosenfield's answer, I would like to add that ByteBuffer.array()
returns the buffer's underlying byte array, which is not necessarily "trimmed" up to the last character. Extra manipulation will be needed, such as the ones mentioned in this answer; in particular:
byte[] b = new byte[bb.remaining()]
bb.get(b);