I am a javacard beginner. And I'm trying to develop javacard applet, but I do not know which function to use when copying arrays, Util.arrayCopy or Util.arrayCopyNonAtomic ? What's the difference between them?

The Java Card Platform provides two methods Util.arrayCopy and Util.arrayCopyNonAtomic for coping data from one byte array to another. I've seen some programmers using Util.arrayCopy anywhere as they don't know the difference between the two methods or why exception is thrown.

In the Java Card API document Util.arrayCopy is described as:

If the destination array is persistent, the entire copy is performed atomically. The copy operation is subject to atomic commit capacity limitations. If the commit capacity is exceeded, no copy is performed and a TransactionException exception is thrown.

and Util.arrayCopyNonAtomic is described as:

If power is lost during the copy operation and the destination array is persistent, a partially changed destination array could result. The copy length parameter is not constrained by the atomic commit capacity limitations.

So the principal diference is that Util.arrayCopy method copy data atomically but Util.arrayCopyNonAtomic doesn't.

Someone may consider that using Util.arrayCopy instead of Util.arrayCopyNonAtomic will not cause problems even if atomic is not needed. But first Util.arrayCopy needs to backup old data in the persistent memory, so it's slower than Util.arrayCopyNonAtomic; and second the size of backup area is limited, using Util.arrayCopy for a large length copy which exceed the commit capacity will cause a TransactionException exception thrown. If you want to copy large length data atomically, you need to design a procedure yourself.

Finally, I think programmer must clearly understand if a data copy should be atomic, and then he/she must choose the correct way to implement it.

Board Disclaimer

The views and comments posted in these fora are personal and do not necessarily represent the those of the Management of JavaCard OS.

The Management of JavaCard OS does not, under any circumstances whatsoever, accept any responsibility for any advice, or recommentations, made by, or implied by, any member or guest vistor of JavaCard OS that results in any loss whatsoever in any manner to a member of JavaCard OS, or to any other person.

Furthermore, the Management of JavaCard OS is not, and cannot be, responsible for the content of any other Internet site(s) that have been linked to from JavaCard OS.