Page 1 of 1
Applet's own space
Posted: Thu Jun 14, 2018 10:01 pm
by hanjing
This problem may be a little strange, but it is indeed an application developer proposed to me.
How can an Applet know how much NVM space it occupies?
I don't want to use JCSystem.getAvailableMemory() to achieve this because its return value is too small, but my card space can be quite large.
Re: Applet's own space
Posted: Fri Jun 15, 2018 4:27 am
by wumindejia
You can keep new space until SystemException.NO_RESOURCE == ex.getReason()
Re: Applet's own space
Posted: Fri Jun 15, 2018 4:50 am
by scplatform
Maybe you can do it like this:
Code: Select all
byte[] buf = apdu.getBuffer();
byte p1 = buf[ISO7816.OFFSET_P1];
switch (buf[ISO7816.OFFSET_INS]) {
case (byte) 0x00:
byte type=0;
if(0==p1)
type=JCSystem.MEMORY_TYPE_PERSISTENT;
else if(1==p1)
type=JCSystem.MEMORY_TYPE_TRANSIENT_RESET;
else if(2==p1)
type=JCSystem.MEMORY_TYPE_TRANSIENT_DESELECT;
else
ISOException.throwIt(ISO7816.SW_WRONG_P1P2);
memsize=JCSystem.getAvailableMemory(type);
allmemsize=memsize;
while(memsize==(short)32767){
if(type==JCSystem.MEMORY_TYPE_PERSISTENT)
bbb=new byte[memsize];
else
bbb=JCSystem.makeTransientByteArray(memsize,type);
memsize=JCSystem.getAvailableMemory(type);
allmemsize+=memsize;
}
bbb=null;
JCSystem.requestObjectDeletion();
buf[3]=(byte)(allmemsize);
buf[2]=(byte)(allmemsize>>8);
buf[1]=(byte)(allmemsize>>16);
buf[0]=(byte)(allmemsize>>24);
//buf[1]=(byte)(memsize>>8);
apdu.setOutgoingAndSend((short)0, (short)4);
break;
default:
// good practice: If you don't know the INStruction, say so:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
Re: Applet's own space
Posted: Fri Jun 15, 2018 5:05 am
by later123654
you can use a new API(since jck3.0.4) to get the size:
public static void getAvailableMemory(short[] buffer,
short offset,
byte memoryType)
throws SystemException
Obtains the amount of memory of the specified type that is available to the applet. Note that implementation-dependent memory overhead structures may also use the same memory pool. The requested memory information is returned as a 32 bit number stored in the specified short array - buffer. The 32 bit number number is the concatenation of buffer[offset] and buffer[offset+1].
Notes:
•The number of bytes returned is only an upper bound on the amount of memory available due to overhead requirements.
•Allocation of CLEAR_ON_RESET transient objects may affect the amount of CLEAR_ON_DESELECT transient memory available.
•Allocation of CLEAR_ON_DESELECT transient objects may affect the amount of CLEAR_ON_RESET transient memory available.
•The 32 bit number is not an indicator of the size of object which may be created since memory fragmentation is possible.
Parameters:memoryType - the type of memory being queried. One of the MEMORY_TYPE_* constants defined above, for example MEMORY_TYPE_PERSISTENT.buffer - the output buffer for storing memory size informationoffset - the offset within the buffer where memory size information beginsThrows:ArrayIndexOutOfBoundsException - if buffer[offset] or buffer[offset+1] outside array boundsNullPointerException - if buffer is nullSystemException - with the following reason codes: •SystemException.ILLEGAL_VALUE if memoryType is not a valid memory type.
Since:3.0.4