In Java 1.8.0, I would like to have the bytes of a String copied to a given array without a new array being returned. I have found three methods which approximate what I would like:
byte[] String.getBytes(String charsetName)
gives the result I would like, but it returns a new array.
void String.getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin)
copies to a given array, but it has no option for choosing the charset, dropping the upper 8 bits of each char
in char[] String.value
. This method also happens to be deprecated.
byte[] StringCoding.encode(Charset cs, char[] ca, int off, int len)
, which is called by the above String.getBytes
, also does the job, but it returns a new array. This method also happens not to be publicly usable.
My use case is iterating over a collection of strings, reading their bytes into an existing structure which does not care about having a unique byte array for every string. One-time allocation of objects at the beginning is okay, but I do not want to allocate objects while iterating over the strings.
Could anyone suggest how I could have the functionality of the second method without the allocation of a new array or any other object?
You can wrap your array in a ByteBuffer, and use a CharsetEncoder directly:
byte[] byteArray = ...;
String charsetName = ...;
ByteBuffer buffer =
ByteBuffer.wrap(byteArray, dstBegin, dstEnd - dstBegin);
CharsetEncoder encoder = Charset.forName(charsetName).newEncoder();
CoderResult result =
encoder.encode(CharBuffer.wrap(string), buffer, true);
if (!result.isUnderflow()) {
result.throwException();
}
Note that ByteBuffer.wrap is lightweight and does not create a new array; it just provides a ByteBuffer instance that delegates its storage to the byte array given to the method.
If you plan to use the entire byte array, you can just use the easier-to-read ByteBuffer.wrap(byteArray)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With