Reading Java Map from Array

I have created a Smartcard application where I can store data up to 60KB in a byte array. But when I read the Array several times, I get an error and I cannot access the data anymore.

The code creates an array:

public void createFile(short fileID, short fileSize) {
    short index = getFileIndex(fileID);

    if(listFiles[index] == null) {
        listFiles[index] = new byte[fileSize];
    }

    listfileSizes[index] = fileSize;
}

      

Reading code data:

public byte[] readDataFromFile(short fileID, short fileOffset, short length) {

    short selFileSize = getFileSize(fileID);
    byte[] data = new byte[length];

    if (selFileSize < (short)(fileOffset + length)) {
        ISOException.throwIt(ISO7816.SW_FILE_FULL); 
    }

    Util.arrayCopy(getFile(fileID), fileOffset, data, (short)0, length);

    return (byte[])data;
}

      

Code Access Reading:

short data_length = Util.getShort(buf, (short)(offset_cdata + 2));
    short file_offset = Util.getShort(buf, offset_cdata);

    if(p2 == (byte)0x01) {

        Util.arrayCopy(myfile.readDataFromFile(myfile.keepassData1, file_offset, data_length), (short)0, buf, (short)0, data_length);

    } else if (p2 == (byte)0x02) {

        Util.arrayCopy(myfile.readDataFromFile(myfile.keepassData2, file_offset, data_length), (short)0, buf, (short)0, data_length);

    }

      

When I reinstall the application, I can read and write, but only a few times until the data is locked. I am getting 6f00 error.

+3


source to share


1 answer


Your applet is running out of persistent memory hence the error.

This line

byte[] data = new byte[length];

      



allocates a new constant byte array every time the method is called! This object is never freed because Java Card does not support automatic garbage collection.

Copy data directly to the APDU buffer:

private final byte[] readDataFromFile(short fileID, short fileOffset, short length, byte[] outBuffer, short outOffset) {
    final short selFileSize = getFileSize(fileID);
    if (selFileSize < (short)(fileOffset + length)) {
        ISOException.throwIt(ISO7816.SW_FILE_FULL); 
    }
    Util.arrayCopyNonAtomic(getFile(fileID), fileOffset, outBuffer, outOffset, length);
}

      

+4


source







All Articles