Write your applet and Applet.process(..) method as you would with any other applets. For consistency, it is advisable that your process(..) code begin like the one below:
public void process(APDU apdu) {
byte[] buffer = apdu.getBuffer();
if (apdu.isISOInterindustryCLA()) {
if (this.selectingApplet()) {
return;
} else {
ISOException.throwIt (ISO7816.SW_CLA_NOT_SUPPORTED);
}
}
switch (buffer[ISO7816.OFFSET_INS]) {
case CHOICE_1:
...
return;
case CHOICE_2:
...
...
default:
ISOException.throwIt (ISO7816.SW_INS_NOT_SUPPORTED);
}
}
For cases 3S, 4S, 3E and 4E, write the method to handle incoming data. Use the API so that your applet properly handles extended, as well as non-extended, cases.
void receiveData(APDU apdu) {
byte[] buffer = apdu.getBuffer();
short LC = apdu.getIncomingLength();
short recvLen = apdu.setIncomingAndReceive();
short dataOffset = apdu.getOffsetCdata();
while (recvLen > 0) {
...
[process data in buffer[dataOffset]...]
...
recvLen = apdu.receiveBytes(dataOffset);
}
// Done
}
For case 2S, 2E, write the method handling data output. A method could look something like this:
void sendData(APDU apdu) {
byte[] buffer = apdu.getBuffer();
short LE = apdu.setOutgoing();
short toSend = ...
if (LE != toSend) {
apdu.setOutgoingLength(toSend);
}
while (toSend > 0) {
...
[prepare data to send in APDU buffer]
...
apdu.sendBytes(dataOffset, sentLen);
toSend -= sentLen;
}
// Done
}