Java Coding Question

Hey all - Admittedly, Java is not my strong suit.

I’m working with the tiny version of the openjavacard-ndef project, where, at the install method, the details for the parameter provided during the app install are identified by the offset:

 public static void install(byte[] buf, short off, byte len) {
        short pos = off;
        // find AID
        byte  lenAID = buf[pos++];
        short offAID = pos;
        pos += lenAID;
        // find control information (ignored)
        byte  lenCI = buf[pos++];
        short offCI = pos;
        pos += lenCI;
        // find applet data
        byte  lenAD = buf[pos++];
        short offAD = pos;
        pos += lenAD;

        // instantiate and initialize the applet
        NdefApplet applet = new NdefApplet(buf, offAD, lenAD);

        // register the applet
        applet.register(buf, offAID, lenAID);
    }

I have an alternate version, where I’m attempting to append hardcoded text to what is received from the parameter, which looks like this:

public static void install(byte[] buf, short off, byte len) {
        
        short pos = off;
        // find AID
        byte  lenAID = buf[pos++];
        short offAID = pos;
        pos += lenAID;
        // find control information (ignored)
        byte  lenCI = buf[pos++];
        short offCI = pos;
        pos += lenCI;
        // find applet data
        byte  lenAD = buf[pos++];
        short offAD = pos;
        pos += lenAD;

        // appending
        byte [] paramData = new byte[lenAD];
        Util.arrayCopyNonAtomic(buf, offAD, paramData, (short) 0, lenAD);

        byte[] scanSuffix = {
        };

        short appendedLength = (short) (lenAD + scanSuffix.length);
        byte[] appendedData = new byte[appendedLength];
        Util.arrayCopyNonAtomic(paramData, (short) 0, appendedData, (short) 0, lenAD);
        Util.arrayCopyNonAtomic(scanSuffix, (short) 0, appendedData, lenAD, (short) scanSuffix.length);

        NdefApplet applet = new NdefApplet(appendedData, (short) 0, (byte) appendedLength);

        // register the applet
        applet.register();
    }

This works without an issue. However, I cannot seem to add content to the scanSuffix variable. I had assumed I could do either:

byte[] scanSuffix = {(byte) 0xAA, (byte) 0xBB};
// or
byte[] scanSuffix = {(byte) 'a', (byte) 'b'};

Either of these convert and install fine, but do not work as expected.

My questions are:

  1. Is there a functional problem with the way I’m adding to the parameter data?
  2. I understand the Tiny variant prevents re-writing of tags, etc. I understood this as after the tag has been installed. Would this have any impact on adding hardcoded text at install time?

I know this isn’t a support forum for java coding, but figured I’d ask :melting_face:

1 Like

Heap allocation using new is not recommended in Javacard development, instead use static arrays and allocate them via JCSystem.makeTransientByteArray in install() .

2 Likes

@StarGate01 - appreciate you jumping in - I assume you’re helping me with this in your message.

The outcome, whether using eeprom or ram gives me the same result - so i don’t think that’s related to the issue I’m experiencing.

I’m just looking to append a string to the parameter data received. Referencing this, both the methods I tried should work.

Not sure if you have any other suggestion specific to what I’m trying to do or if I misunderstand.

Updating this because I believe I found the cause.

The code in my initial message worked, as no data was being added to the record.

I had assumed the parameter passed was only the URI, but it is in fact the entire URI record, meaning my code was taking:

NDEF Uri Record ID '1' Resource 'https://mysite.com'

And appending to it, which meant it was doing:

NDEF Uri Record ID '1' Resource 'https://mysite.com'/extracontent

The solution would be to replace at the last character (the apostrophe) and include an apostrophe within my appended content.

2 Likes