ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(signatureRecord.getVersion());
if(!signatureRecord.hasSignatureType()) {
throw new NdefEncoderException("Expected signature type", signatureRecord);
}
if(signatureRecord.hasSignature() && signatureRecord.hasSignatureUri()) {
throw new NdefEncoderException("Expected signature or signature uri, not both", signatureRecord);
} else if(!signatureRecord.hasSignature() && !signatureRecord.hasSignatureUri()) {
throw new NdefEncoderException("Expected signature or signature uri", signatureRecord);
}
baos.write(((signatureRecord.hasSignatureUri() ? 1 : 0) << 7) | (signatureRecord.getSignatureType().getValue() & 0x7F));
byte[] signatureOrUri;
if(signatureRecord.hasSignature()) {
signatureOrUri = signatureRecord.getSignature();
if(signatureOrUri.length > 65535) {
throw new NdefEncoderException("Expected signature size " + signatureOrUri.length + " <= 65535", signatureRecord);
}
} else {
signatureOrUri = signatureRecord.getSignatureUri().getBytes(NdefConstants.UTF_8_CHARSET);
if(signatureOrUri.length > 65535) {
throw new NdefEncoderException("Expected signature uri byte size " + signatureOrUri.length + " <= 65535", signatureRecord);
}
}
baos.write((signatureOrUri.length >> 8) & 0xFF);
baos.write(signatureOrUri.length & 0xFF);
baos.write(signatureOrUri);
if(!signatureRecord.hasCertificateFormat()) {
throw new NdefEncoderException("Expected certificate format", signatureRecord);
}
List<byte[]> certificates = signatureRecord.getCertificates();
if(certificates.size() > 16) {
throw new NdefEncoderException("Expected number of certificates " + certificates.size() + " <= 15", signatureRecord);
}
CertificateFormat certificateFormat = signatureRecord.getCertificateFormat();
baos.write(((signatureRecord.hasCertificateUri() ? 1 : 0) << 7) | (certificateFormat.getValue() << 4) | (certificates.size() & 0xF));
for(int i = 0; i < certificates.size(); i++) {
byte[] certificate = certificates.get(i);
if(certificate.length > 65535) {
throw new NdefEncoderException("Expected certificate " + i + " size " + certificate.length + " <= 65535", signatureRecord);
}
baos.write((certificate.length >> 8) & 0xFF);
baos.write(certificate.length & 0xFF);
baos.write(certificate);
}
if(signatureRecord.hasCertificateUri()) {
byte[] certificateUri = signatureRecord.getCertificateUri().getBytes(NdefConstants.UTF_8_CHARSET);
if(certificateUri.length > 65535) {
throw new NdefEncoderException("Expected certificate uri byte size " + certificateUri.length + " <= 65535", signatureRecord);
}
baos.write((certificateUri.length >> 8) & 0xFF);
baos.write(certificateUri.length & 0xFF);
baos.write(certificateUri);