Creating an Applet That Can Send and Receive Extended Length APDUs

To create an applet that can send and receive extended length APDUs:

  1. Implement the javacardx.apdu.ExtendedLength interface in your applet:
    ...
    import javacard.framework.*;
    import javacardx.apdu.ExtendedLength;
    ...
    public MyApplet extends Applet implements 
    ExtendedLength {
    ...
    }
    
    
  2. 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);
       }
    }
    
    
    
  3. 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 
    }
    
    
  4. 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
    }