2

I need to get bytes of millions of string using this :

String str="blablabla...."; // some UTF-16LE encoding string extracted from DB
bytes=str.getBytes("UTF-16LE")

But this is awfully slow. There are custom fast versions of getBytes but they don't support UTF-16LE. For example this is one of them:

// http://stackoverflow.com/questions/12239993/why-is-the-native-string-getbytes-method-slower-than-the-custom-implemented-getb
private static byte[] getBytesFast(String str) {
    final char buffer[] = new char[str.length()];
    final int length = str.length();
    str.getChars(0, length, buffer, 0);
    final byte b[] = new byte[length];
    for (int j = 0; j < length; j++)
        b[j] = (byte) buffer[j];
    return b;
}

Is there similar fast solution to convert the Java string into byte array using UTF-16LE encoding?

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
AVEbrahimi
  • 17,993
  • 23
  • 107
  • 210

1 Answers1

3

This version will produce UTF16LE bytes array:

private static byte[] getBytesUTF16LE(String str) {
    final int length = str.length();
    final char buffer[] = new char[length];
    str.getChars(0, length, buffer, 0);
    final byte b[] = new byte[length*2];
    for (int j = 0; j < length; j++) {
        b[j*2] = (byte) (buffer[j] & 0xFF);
        b[j*2+1] = (byte) (buffer[j] >> 8);
    }
    return b;
}

Tested:

String test = "UTF16 Ελληνικά Русский 日本語";
byte[] bytes = test.getBytes("UTF-16LE");
byte[] bytes2 = getBytesUTF16LE(test);
System.out.println(Arrays.equals(bytes, bytes2));
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334