java: converting part of a ByteBuffer to a string
Posted
by Jason S
on Stack Overflow
See other posts from Stack Overflow
or by Jason S
Published on 2010-06-07T17:59:53Z
Indexed on
2010/06/07
18:12 UTC
Read the original article
Hit count: 197
I have a ByteBuffer containing bytes that were derived by String.getBytes(charsetName)
, where "containing" means that the string comprises the entire sequence of bytes between the ByteBuffer's position()
and limit()
.
What's the best way for me to get the string back? (assuming I know the encoding charset) Is there anything better than the following (which seems a little clunky)
byte[] ba = new byte[bbuf.remaining()];
bbuf.get(ba);
try {
String s = new String(ba, charsetName);
}
catch (UnsupportedEncodingException e) {
/* take appropriate action */
}
© Stack Overflow or respective owner