Java 卡从数组中读取数据
Java Card Read Data from Array
我创建了一个智能卡应用程序,我可以在其中以字节数组形式存储最多 60KB 的数据。但是当我多次读取数组时出现错误,我无法再访问数据。
代码创建数组:
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.
您的小程序 运行 超出永久内存,因此出现错误。
这一行
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);
}
我创建了一个智能卡应用程序,我可以在其中以字节数组形式存储最多 60KB 的数据。但是当我多次读取数组时出现错误,我无法再访问数据。
代码创建数组:
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.
您的小程序 运行 超出永久内存,因此出现错误。
这一行
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);
}