What is the difference between Util.arrayCopy and Util.arrayCopyNonAtomic?

JavaCard Applet Development Related Questions and Answers.
User avatar
Posts: 2
Joined: Wed May 20, 2015 3:57 am
Points: 0

What is the difference between Util.arrayCopy and Util.arrayCopyNonAtomic?

Postby Peterson » Thu May 28, 2015 1:27 am

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?

Can anyone give me some brief idea? thanks

User avatar
Posts: 27
Joined: Wed May 20, 2015 2:41 am
Points: 156

Re: What is the difference between Util.arrayCopy and Util.arrayCopyNonAtomic?

Postby Tolice » Thu May 28, 2015 2:12 am

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.

Return to “Questions & Answers”

Who is online

Users browsing this forum: No registered users and 4 guests

JavaCard OS : Disclaimer