我正在编写一个小程序,它存储 3 个不同大小的文件:5 Kb、7 Kb 和 11 Kb。我在小程序中存储文件没有任何问题。但是当我尝试读回它们时,我只能读取前两个(较小的文件)。
第三个文件抛出异常:
javax.smartcardio.CardException: Could not obtain response
at sun.security.smartcardio.ChannelImpl.doTransmit(Unknown Source)
at sun.security.smartcardio.ChannelImpl.transmit(Unknown Source)
我试图找出问题所在,发现它与文件的大小有关。因此,我创建了一个大小为 7 Kb 的测试文件,并一点一点地递增该文件。
它一直工作到我达到 7905 字节。这意味着 7905 字节是我可以从小程序读取的最大字节数。
我使用示例代码链接响应:
public void readFile(APDU apdu, short[] offset, short selectedFile, short MAX_APDU_SEN, byte OFFSET_SENT) {
byte[] file = getFile(selectedFile);
if (file == null) {
+ ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);+
}
// work out how many bytes to send this time and how many will be left
short remain = (short) (file.length - offset[OFFSET_SENT]);
boolean chain = remain > MAX_APDU_SEN;
short sendLen = chain ? MAX_APDU_SEN : remain;
apdu.setOutgoing();
apdu.setOutgoingLength(sendLen);
apdu.sendBytesLong(file, offset[OFFSET_SENT], sendLen);
// Check to see if there are more APDU's to send
if (chain) {
+offset[OFFSET_SENT] = sendLen; // count the bytes sent
ISOException.throwIt(ISO7816.SW_BYTES_REMAINING_00); // indicate there are more bytes to come
} else {+
offset[OFFSET_SENT] = 0; // no more bytes to send
}
}
我尝试过两种不同类型的卡,即 JC 2.2.1 (36Kb) 和 JC 2.2.2 (80Kb) 兼容卡,但它们的行为都是相同的。
有什么帮助吗?
读取文件通常不使用链接来完成,因为主机应用程序可以方便地指定 P1/P2 中的起始偏移量 - 至少在 ISO 7816-4 中指定的 READ BINARY 命令中如此。我认为,即使对于链式响应,卡也希望在缓冲区中准备数据,我认为缓冲区的有限大小是您问题的原因。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)