/** * Returns a byte array representation of this entire NDEF message. */ public byte[] toByteArray() { //TODO: allocate the byte array once, copy each record once //TODO: process MB and ME flags outside loop if ((mRecords == null) || (mRecords.length == 0)) return new byte[0]; byte[] msg = {}; for (int i = 0; i < mRecords.length; i++) { byte[] record = mRecords[i].toByteArray(); byte[] tmp = new byte[msg.length + record.length]; /* Make sure the Message Begin flag is set only for the first record */ if (i == 0) { record[0] |= FLAG_MB; } else { record[0] &= ~FLAG_MB; } /* Make sure the Message End flag is set only for the last record */ if (i == (mRecords.length - 1)) { record[0] |= FLAG_ME; } else { record[0] &= ~FLAG_ME; } System.arraycopy(msg, 0, tmp, 0, msg.length); System.arraycopy(record, 0, tmp, msg.length, record.length); msg = tmp; } return msg; }