javafunctionjavacardapduiso-7816-4

Re writing Funcition with APDU & Boolean


I need some help

I need that this function return a boolean if while is executed or no.

Could you help me?


Solution

  • Let us rewrite that APDU example from the API because it is clearly broken. It will generate a separate request to receiveBytes when it is not required.

    public boolean receiveIncomingFNEL(APDU apdu) {
        byte[] buf = apdu.getBuffer();
    
        short bytesRead = apdu.setIncomingAndReceive();
        // NOTE: we must always call setIncomingAndReceive() first
        short offsetCdata = apdu.getOffsetCdata();
        short bytesLeft = apdu.getIncomingLength() - bytesRead;
        boolean receiveBytesRequired = true;
        while (true) {
            // --- do something with the bytes in buf, offsetCdata and bytesRead here
            if (bytesLeft <= 0) {
                 receiveBytesRequired = false;
                 break;
            }
            bytesRead = apdu.receiveBytes(offsetCdata);
            bytesLeft -= bytesRead;
        }
        // --- do other stuff
        return receiveBytesRequired;
    }
    

    The if & break statements in the while() loop are a bit weird, but that's the only way that I could think of to have one place handling the data in the APDU buffer and one part where receiveBytes() is called. This is because setIncomingAndReceive() includes a receiveBytes() within the call (ideas to create a normal while loop welcome).