0

I am trying to put a string buffer in a bytebuffer..

out.put(ss);

where out is a ByteBuffer and ss is a StringBuffer. Is there any easy way to do this? Assuming ASCII encoding for the string.

Thanks

1 Answer 1

2
StringBuffer buf = ...;
byte[] bytes = buf.toString().getBytes("US-ASCII");

or possibly "ISO-8859-1"

Sign up to request clarification or add additional context in comments.

1 Comment

Does this pollute the String space in Java? I've heard that this can be bad for memory reasons; by calling "toString" on large bytebuffers (many MB) we cause these strings to live forever in the JVM. Am I making this up? Is there anyway to go straight from StringBuffer to Bytes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.