2017-06-01 128 views
3

我創建了一個智能卡應用程序,我可以在字節數組中存儲高達60KB的數據。但是當我多次閱讀數組時,我得到一個錯誤,我不能再訪問數據。Java卡從數組中讀取數據

代碼創建數組:

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

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

    listfileSizes[index] = fileSize; 
} 

代碼讀取數據:

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; 
} 

代碼訪問閱讀:

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); 

    } 

當我重新安裝應用程序,我可以讀,寫,但只幾次直到數據被阻止。我得到錯誤6f00。

回答

4

您的小應用程序是用盡永久內存,因此錯誤。

此行

byte[] data = new byte[length]; 

每個方法被調用時分配一個新的持久的字節數組!該對象永遠不會被釋放,因爲Java Card不支持任何自動垃圾收集器。

你應該直接將數據複製到APDU緩衝器:

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); 
} 
+0

好還得感謝我會嘗試 – schisskiss