JavaCard中如何存储大于128字节的数据
我无法在字节数组中索引高于 128 处写入数据。 代码如下。
private void Write1(APDU apdu) throws ISOException
{
apdu.setIncomingAndReceive();
byte[] apduBuffer = apdu.getBuffer();
byte j = (byte)apduBuffer[4]; // Return incoming bytes lets take 160
Buffer1 = new byte[j]; // initialize a array with size 160
for (byte i=0; i<j; i++)
Buffer1[(byte)i] = (byte)apduBuffer[5+i];
}
它给我错误 6F 00 (这意味着到达文件结尾)。
我正在使用:
- 智能卡类型=
- 使用java卡2.2.2的接触卡和使用apdu的jcop
I can't write data at index above 128 in byte array.
code is given below.
private void Write1(APDU apdu) throws ISOException
{
apdu.setIncomingAndReceive();
byte[] apduBuffer = apdu.getBuffer();
byte j = (byte)apduBuffer[4]; // Return incoming bytes lets take 160
Buffer1 = new byte[j]; // initialize a array with size 160
for (byte i=0; i<j; i++)
Buffer1[(byte)i] = (byte)apduBuffer[5+i];
}
It gives me error 6F 00 (It means reach End Of file).
I am using:
- smart card type = contact card
- using java card 2.2.2 with jcop using apdu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码包含几个问题:
正如 'pst' 已经指出的那样,您使用的是带符号的
byte
值,该值最多只能工作到 128 - 请改用short
< /p>您正在创建一个每个新缓冲区
Buffer1
调用您的Write1
方法。在 JavaCard 上通常没有自动垃圾收集 - 因此内存分配只应在应用程序安装时进行一次。如果您只想处理 adpu 缓冲区中的数据,只需从那里使用它即可。如果您想将数据从一个字节数组复制到另一个字节数组中,最好使用 javacard.framework.Util.arrayCopy(..)。您正在调用
apdu.setIncomingAndReceive();
但忽略返回值。返回值给出了可以读取的数据字节数。以下代码来自 API 文档,展示了常见的方式:
Your code contains several problems:
As already pointed out by 'pst' you are using a signed
byte
value which works only up to 128 - use ashort
insteadYour are creating a new buffer
Buffer1
on every call of yourWrite1
method. On JavaCard there is usually no automatic garbage collection - therefore memory allocation should only be done once when the app is installed. If you only want to process the data in the adpu buffer just use it from there. And if you want to copy data from one byte array into another better usejavacard.framework.Util.arrayCopy(..)
.You are calling
apdu.setIncomingAndReceive();
but ignore the return value. The return value gives you the number of bytes of data you can read.The following code is from the API docs and shows the common way:
详细阐述 pst 的答案。一个字节有 2^8 位数字,或者更确切地说是 256 位数字。但如果您使用有符号数字,它们将在一个循环中工作。因此,128 实际上将是 -128,129 将是 -127,依此类推。
Elaborating on pst's answer. A byte has 2^8 bits numbers, or rather 256. But if you are working with signed numbers, they will work in a cycle instead. So, 128 will be actually -128, 129 will be -127 and so on.
更新:虽然以下答案对于普通 Java 是“有效”的,但请参阅 Roberts 答案以获取 Java 卡特定信息以及其他问题/方法。
在 Java 中,
byte
的值在[-128, 127]
范围内,因此,当您说“160”时,这不是代码的内容真的给了你:)也许你想使用:
将值
apduBuffer[4]
“向上转换”为int
,同时将原始字节数据视为< em>未签名 价值。同样,
i
也应该是int
(以避免讨厌的溢出和循环永远错误),并且 System.arraycopy 方法也很方便...(我不知道这是否是唯一/真正的问题 - 或者上面的方法是否是 Java Card - 但这肯定是一个问题并且符合“128限制” ”提到。)
快乐编码。
Update: While the following answer is "valid" for normal Java, please refer to Roberts answer for Java Card-specific information, as well additional concerns/approaches.
In Java a
byte
has values in the range[-128, 127]
so, when you say "160", that's not what the code is really giving you :)Perhaps you'd like to use:
That "upcasts" the value
apduBuffer[4]
to anint
while treating the original byte data as an unsigned value.Likewise,
i
should also be anint
(to avoid a nasty overflow-and-loop-forever bug), and the System.arraycopy method could be handy as well...(I have no idea if that is the only/real problem -- or if the above is a viable solution on a Java Card -- but it sure is a problem and aligns with the "128 limit" mentioned.)
Happy coding.