ByteBuffer Little Endian insert not working

后端 未结 2 1571
隐瞒了意图╮
隐瞒了意图╮ 2021-02-18 17:11

I have to make a two way communication between a legacy system and an android device. The legacy system uses little endian byte ordering. I have successfully implemented the rec

2条回答
  •  旧时难觅i
    2021-02-18 17:39

    On a related note:

    This code:

     int unicodePointsLen = textContent.length() * 2;
     ByteBuffer unicodePointsBuffer = ByteBuffer.allocateDirect(unicodePointsLen);
     short unicodePointValue;
     for (int i = 0; i < textContent.length(); i++) 
     {  
         unicodePointValue = (short)textContent.charAt(i);
         unicodePointsBuffer.put((byte)(unicodePointValue & 0x00FF)).put((byte)(unicodePointValue >> 8));
     }
    

    Is about 25% faster than this:

     int unicodePointsLen = textContent.length() * 2;
     ByteBuffer unicodePointsBuffer = ByteBuffer.allocateDirect(unicodePointsLen);
     unicodePointsBuffer.order(ByteOrder.LITTLE_ENDIAN);
     for (int i = 0; i < textContent.length(); i++) 
     {  
         unicodePointsBuffer.putShort((short)textContent.charAt(i));  
     }
    

    Using JDK 1.8.

    I am trying to pass unicode points from JAVA to C++ through JNI and the first method is the fastest I found. Curious that it is faster than the second snippet.

提交回复
热议问题