Page 1 of 1

A problem about storing data larger than 128 bytes in javacard

Posted: Wed Dec 23, 2015 2:02 am
by Diorin
I run into a problem when storing data larger than 128 bytes in javacard.

part of my code

Code: Select all

private void Write1(APDU apdu) throws ISOException
{
    apdu.setIncomingAndReceive();
    byte[] apduBuffer = apdu.getBuffer();
    byte j = (byte)apduBuffer[4];       // Return incoming bytes lets take 160
    Buffer1 = new byte[j];              // initialize a array with size 160
    for (byte i=0; i<j; i++)
        Buffer1[(byte)i] = (byte)apduBuffer[5+i];
}


When I run my applet, it returns sw=0x6F00. How can I modify my code so that it can work well?

Re: A problem about storing data larger than 128 bytes in javacard

Posted: Wed Dec 30, 2015 10:10 pm
by Tarantino
There is some problems in your code.
-You are creating a new buffer Buffer1 on every call of your Write1 method. On JavaCard there is usually no automatic garbage collection - therefore memory allocation should only be done once when the app is installed. If you only want to process the data in the adpu buffer just use it from there. And if you want to copy data from one byte array into another better use javacard.framework.Util.arrayCopy().

-You are calling apdu.setIncomingAndReceive(); but ignore the return value. The return value gives you the number of bytes of data you can read.