Fast way to convert byte[] to short[] on Java Card

别说谁变了你拦得住时间么 提交于 2019-12-05 13:29:34
vojta

Do not reinvent the wheel: there are useful built-in static methods in the Java Card API, often implemented as native functions for performance reasons. Your code cannot be better than them.

1) First of all, javacardx.framework.util.ArrayLogic.arrayCopyRepackNonAtomic is what you need when working with a RAM array:

ArrayLogic.arrayCopyRepackNonAtomic(b, (short) 0, len, temp_conv, (short) 0);

There is also arrayCopyRepack, which is useful for persistent arrays (the whole operation is done in a single transaction, but it is a little slower).


2) If you cannot use ArrayLogic, there is always javacard.framework.Util.getShort, which you can use instead of the bitwise magic:

private static final void byteToShort(final byte[] bytes, final short blen, final short[] shorts)
{
    short x = 0;
    short y = 0;
    for (; y < blen; x++, y += 2)
    {
        shorts[x] = Util.getShort(bytes, y);
    }
}

Note there is also setShort, which might be useful for short[] to byte[] conversion.


3) Some other notes on your code in case you really want to implement it on your own:

  • Your aux is stored in the persistent memory. This is awfully slow and it will probably damage your card, because aux is rewritten very often, see Symptoms of EEPROM damage.
  • b[2*x+j] is not effective, because of slow multiplication. You should use two loop variables instead and addition only.
  • Get rid of aux and the inner loop, you don't need them at all
  • What about int len? There is no int in Java Card 2.2.2...
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!