我创建了一个智能卡应用程序,我可以在字节数组中存储高达60KB的数据。但是当我多次阅读Array时,我收到了一个错误,我再也无法访问数据了。
代码创建数组:
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。
答案 0 :(得分: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);
}