Package org.nfctools.ndef

Examples of org.nfctools.ndef.NdefEncoderException


    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    // cps
    CarrierPowerState carrierPowerState = alternativeCarrierRecord.getCarrierPowerState();
    if (carrierPowerState == null) {
      throw new NdefEncoderException("Expected carrier power state", alternativeCarrierRecord);
    }
    bout.write(carrierPowerState.getValue() & 0x7); // 3 lsb

    // carrier data reference: 1
    String carrierDataReference = alternativeCarrierRecord.getCarrierDataReference();
    if (carrierDataReference == null) {
      throw new NdefEncoderException("Expected carrier data reference", alternativeCarrierRecord);
    }
    byte[] carrierDataReferenceChar = carrierDataReference.getBytes(NdefConstants.DEFAULT_CHARSET);
    if (carrierDataReferenceChar.length > 255) {
      throw new NdefEncoderException("Expected carrier data reference '" + carrierDataReference
          + "' <= 255 bytes", alternativeCarrierRecord);
    }
    // carrier data reference length (1)
    bout.write(carrierDataReferenceChar.length);
    // carrier data reference char
    bout.write(carrierDataReferenceChar, 0, carrierDataReferenceChar.length);

    // auxiliary data
    List<String> auxiliaryDataReferences = alternativeCarrierRecord.getAuxiliaryDataReferences();
    // auxiliary data reference count
    bout.write(auxiliaryDataReferences.size());

    for (String auxiliaryDataReference : auxiliaryDataReferences) {

      byte[] auxiliaryDataReferenceChar = auxiliaryDataReference.getBytes(NdefConstants.DEFAULT_CHARSET);
      // carrier data reference length (1)

      if (auxiliaryDataReferenceChar.length > 255) {
        throw new NdefEncoderException("Expected auxiliary data reference '" + auxiliaryDataReference
            + "' <= 255 bytes", alternativeCarrierRecord);
      }

      bout.write(auxiliaryDataReferenceChar.length);
      // carrier data reference char
View Full Code Here


    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    CarrierTypeFormat carrierTypeFormat = handoverCarrierRecord.getCarrierTypeFormat();
    if (carrierTypeFormat == null) {
      throw new NdefEncoderException("Expected carrier type format", handoverCarrierRecord);
    }
    bout.write(carrierTypeFormat.getValue() & 0x7);

    Object carrierType = handoverCarrierRecord.getCarrierType();

    byte[] encoded;

    switch (carrierTypeFormat) {
      case WellKnown: {
        // NFC Forum well-known type [NFC RTD]
        if (carrierType instanceof WellKnownRecord) {
          WellKnownRecord abstractWellKnownRecord = (WellKnownRecord)carrierType;

          encoded = messageEncoder.encodeSingle(abstractWellKnownRecord);

          break;
        }
        else {
          throw new NdefEncoderException("Expected well-known record to be of supertype " + WellKnownRecord.class.getName(), handoverCarrierRecord);
        }
      }
      case Media: {
        // Media-type as defined in RFC 2046 [RFC 2046]
        String string = (String)carrierType;

        encoded = string.getBytes(NdefConstants.DEFAULT_CHARSET);

        break;
      }
      case AbsoluteURI: {
        // Absolute URI as defined in RFC 3986 [RFC 3986]
        String string = (String)carrierType;

        encoded = string.getBytes(NdefConstants.DEFAULT_CHARSET);

        break;
      }
      case External: {
        // NFC Forum external type [NFC RTD]
        if (carrierType instanceof ExternalTypeRecord) {
          ExternalTypeRecord externalTypeRecord = (ExternalTypeRecord)carrierType;

          encoded = messageEncoder.encodeSingle(externalTypeRecord);

          break;
        }
        else {
          throw new NdefEncoderException("Expected external type record to be of supertype " + ExternalTypeRecord.class.getName(), handoverCarrierRecord);
        }
      }
      default: {
        throw new RuntimeException();
      }
    }

    if (encoded.length > 255) {
      throw new NdefEncoderException("Carrier type 255 byte limit exceeded.", handoverCarrierRecord);
    }
    bout.write(encoded.length);
    bout.write(encoded, 0, encoded.length);

    if (handoverCarrierRecord.hasCarrierData()) {
View Full Code Here

  public byte[] encodePayload(WellKnownRecord wellKnownRecord, NdefMessageEncoder messageEncoder) {

    ErrorRecord errorRecord = (ErrorRecord)wellKnownRecord;

    if (!errorRecord.hasErrorReason()) {
      throw new NdefEncoderException("Expected error reason", wellKnownRecord);
    }

    if (!errorRecord.hasErrorData()) {
      throw new NdefEncoderException("Expected error data", wellKnownRecord);
    }

    ErrorReason errorReason = errorRecord.getErrorReason();

    switch (errorReason) {
      case TemporaryMemoryConstraints: {
        /**
         * An 8-bit unsigned integer that expresses the minimum number of milliseconds after which a Handover
         * Request Message with the same number of octets might be processed successfully. The number of
         * milliseconds SHALL be determined by the time interval between the sending of the error indication and
         * the subsequent receipt of a Handover Request Message by the Handover Selector.
         */

        return new byte[] { errorReason.getValue(), (byte)(errorRecord.getErrorData().shortValue() & 0xFF) };
      }
      case PermanenteMemoryConstraints: {

        /**
         * A 32-bit unsigned integer, encoded with the most significant byte first, that indicates the maximum
         * number of octets of an acceptable Handover Select Message. The number of octets SHALL be determined
         * by the total length of the NDEF message, including all header information.
         */
        long unsignedInt = errorRecord.getErrorData().longValue();
        return new byte[] { errorReason.getValue(), (byte)((unsignedInt >> 24) & 0xFF),
            (byte)((unsignedInt >> 16) & 0xFF), (byte)((unsignedInt >> 8) & 0xFF),
            (byte)(unsignedInt & 0xFF) };
      }
      case CarrierSpecificConstraints: {

        /**
         * An 8-bit unsigned integer that expresses the minimum number of milliseconds after which a Handover
         * Request Message might be processed successfully. The number of milliseconds SHALL be determined by
         * the time interval between the sending of the error indication and the subsequent receipt of a
         * Handover Request Message by the Handover Selector.
         */

        return new byte[] { errorReason.getValue(), (byte)(errorRecord.getErrorData().shortValue() & 0xFF) };
      }
    }

    throw new NdefEncoderException("Unknown error reason " + errorReason, wellKnownRecord);
  }
View Full Code Here

    // major version, minor version
    payload.write((handoverRequestRecord.getMajorVersion() << 4) | handoverRequestRecord.getMinorVersion());

    if (!handoverRequestRecord.hasCollisionResolution()) {
      throw new NdefEncoderException("Expected collision resolution", handoverRequestRecord);
    }

    // implementation note: write alternative carriers and and collision resolution together
    if (!handoverRequestRecord.hasAlternativeCarriers()) {
      // At least a single alternative carrier MUST be specified by the Handover Requester.
      throw new NdefEncoderException("Expected at least one alternative carrier", handoverRequestRecord);
    }
    List<Record> records = new ArrayList<Record>();

    // a collision resolution record
    records.add(handoverRequestRecord.getCollisionResolution());
View Full Code Here

  @Override
  public byte[] encodePayload(WellKnownRecord wellKnownRecord, NdefMessageEncoder messageEncoder) {
    UriRecord uriRecord = (UriRecord)wellKnownRecord;
   
    if(!uriRecord.hasUri()) {
      throw new NdefEncoderException("Expected URI", wellKnownRecord);
    }

    String uri = uriRecord.getUri();
    byte[] uriAsBytes = getUriAsBytes(uri);
View Full Code Here

    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);
    }

    return payload;
  }
View Full Code Here

  @Override
  public NdefRecord encodeRecord(Record record, NdefMessageEncoder messageEncoder) {
    AbsoluteUriRecord absoluteUriRecord = (AbsoluteUriRecord)record;
   
    if(!absoluteUriRecord.hasUri()) {
      throw new NdefEncoderException("Expected URI", record);
    }
   
    return new NdefRecord(NdefConstants.TNF_ABSOLUTE_URI, AbsoluteUriRecord.TYPE, absoluteUriRecord.getId(),
        absoluteUriRecord.getUri().getBytes(NdefConstants.DEFAULT_CHARSET));
  }
View Full Code Here

  @Override
  public NdefRecord encodeRecord(Record record, NdefMessageEncoder messageEncoder) {
    MimeRecord mimeRecord = (MimeRecord)record;

    if(!mimeRecord.hasContentType()) {
      throw new NdefEncoderException("Expected content type", mimeRecord);
    }
   
    return new NdefRecord(NdefConstants.TNF_MIME_MEDIA, mimeRecord.getContentType().getBytes(
            NdefConstants.DEFAULT_CHARSET), record.getId(), mimeRecord.getContentAsBytes());
  }
View Full Code Here

  public byte[] encodePayload(WellKnownRecord wellKnownRecord, NdefMessageEncoder messageEncoder) {

    TextRecord textRecord = (TextRecord)wellKnownRecord;

    if(!textRecord.hasLocale()) {
      throw new NdefEncoderException("Expected locale", wellKnownRecord);
    }

    if(!textRecord.hasEncoding()) {
      throw new NdefEncoderException("Expected encoding", wellKnownRecord);
    }

    if(!textRecord.hasText()) {
      throw new NdefEncoderException("Expected text", wellKnownRecord);
    }

    Locale locale = textRecord.getLocale();

    byte[] languageData = (locale.getLanguage() + (locale.getCountry() == null || locale.getCountry().length() == 0 ? ""
        : ("-" + locale.getCountry()))).getBytes();

    if (languageData.length > TextRecord.LANGUAGE_CODE_MASK) {
      throw new NdefEncoderException("language code length longer than 2^5. this is not supported.", wellKnownRecord);
    }
   
    Charset encoding = textRecord.getEncoding();

    byte[] textData = getTextAsBytes(textRecord, encoding);
View Full Code Here

  public NdefRecord encodeRecord(Record record, NdefMessageEncoder messageEncoder) {

    byte[] key = record.getId();
    if(key != null) {
      if(key.length > 255) {
        throw new NdefEncoderException("Expected record id length <= 255 bytes", record);
      }
    }
   
    WellKnownRecordConfig config = knownRecordTypes.get(record.getClass());
    byte[] payload = config.getPayloadEncoder().encodePayload((WellKnownRecord)record, messageEncoder);
View Full Code Here

TOP

Related Classes of org.nfctools.ndef.NdefEncoderException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.