private short receiveData(APDU apdu, byte[] data) {
        byte[] buffer = apdu.getBuffer();
        short lc = (short)(buffer[ISO7816.OFFSET_LC] & 0x00FF);
        short l;
        short len;

        if (lc > 254) {
            ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
        }
        l = apdu.setIncomingAndReceive();
        Util.arrayCopy(buffer, (short)ISO7816.OFFSET_CDATA, data, (short)0, l);
        lc -= l;
        len = l;
        while (lc > 0) {
            l = apdu.receiveBytes(ISO7816.OFFSET_CDATA);
            Util.arrayCopy(buffer, (short)ISO7816.OFFSET_CDATA, data, len, l);
            lc -= l;
            len += l;
        }
        return len;
    }