简体   繁体   中英

Fast conversion of String to byte[] using UTF-16LE encoding

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?

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM