public class GcActionRecordEncoder implements WellKnownRecordPayloadEncoder {
@Override
public byte[] encodePayload(WellKnownRecord wellKnownRecord, NdefMessageEncoder messageEncoder) {
GcActionRecord actionRecord = (GcActionRecord)wellKnownRecord;
byte[] payload = null;
if (actionRecord.hasAction() && actionRecord.hasActionRecord()) {
throw new NdefEncoderException("Expected action or action record, not both.", wellKnownRecord);
}
if (actionRecord.hasAction()) {
payload = new byte[2];
payload[0] = GcActionRecord.NUMERIC_CODE;
payload[1] = (byte)actionRecord.getAction().getValue();
}
else if (actionRecord.hasActionRecord()) {
byte[] subPayload = messageEncoder.encodeSingle(actionRecord.getActionRecord());
payload = new byte[subPayload.length + 1];
payload[0] = 0;
System.arraycopy(subPayload, 0, payload, 1, subPayload.length);
} else {
throw new NdefEncoderException("Expected action or action record.", wellKnownRecord);