Package org.nfctools.ndef.wkt.handover.records

Examples of org.nfctools.ndef.wkt.handover.records.HandoverCarrierRecord


public class HandoverCarrierRecordEncoder implements WellKnownRecordPayloadEncoder {

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

    HandoverCarrierRecord handoverCarrierRecord = (HandoverCarrierRecord)wellKnownRecord;

    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()) {
      bout.write(handoverCarrierRecord.getCarrierData(), 0, handoverCarrierRecord.getCarrierDataSize());
    }

    return bout.toByteArray();
  }
View Full Code Here


public class HandoverCarrierRecordDecoder implements WellKnownRecordPayloadDecoder {

  @Override
  public WellKnownRecord decodePayload(byte[] payload, NdefMessageDecoder messageDecoder) {

    HandoverCarrierRecord handoverCarrierRecord = new HandoverCarrierRecord();

    CarrierTypeFormat carrierTypeFormat = CarrierTypeFormat.toCarrierTypeFormat((byte)(payload[0] & 0x7));

    handoverCarrierRecord.setCarrierTypeFormat(carrierTypeFormat);

    int carrierTypeLength = (int)(payload[1] & 0xFF);
    byte[] carrierType = new byte[carrierTypeLength];
    System.arraycopy(payload, 2, carrierType, 0, carrierTypeLength);

    switch (carrierTypeFormat) {
      case WellKnown: {
        // NFC Forum well-known type [NFC RTD]

        Record record = messageDecoder.decodeToRecord(carrierType);

        if (record instanceof WellKnownRecord) {
          handoverCarrierRecord.setCarrierType(record);
        }
        else {
          throw new IllegalArgumentException("Expected well-known type carrier type");
        }

        break;
      }
      case Media: {

        // Media-type as defined in RFC 2046 [RFC 2046]
        handoverCarrierRecord.setCarrierType(new String(carrierType, NdefConstants.DEFAULT_CHARSET));

        break;
      }
      case AbsoluteURI: {
        // Absolute URI as defined in RFC 3986 [RFC 3986]
        handoverCarrierRecord.setCarrierType(new String(carrierType, NdefConstants.DEFAULT_CHARSET));

        break;
      }
      case External: {
        // NFC Forum external type [NFC RTD]

        Record record = messageDecoder.decodeToRecord(carrierType);

        if (record instanceof UnsupportedExternalTypeRecord) {
          handoverCarrierRecord.setCarrierType(record);
        }
        else {
          throw new IllegalArgumentException("Expected external type carrier type");
        }
      }
      default: {
        throw new RuntimeException();
      }

    }

    // The number of CARRIER_DATA octets is equal to the NDEF record PAYLOAD_LENGTH minus the CARRIER_TYPE_LENGTH minus 2.   
    int carrierDataLength = payload.length - 2 - carrierTypeLength;

    byte[] carrierData;
    if (carrierDataLength > 0) {
      carrierData = new byte[carrierDataLength];
      System.arraycopy(payload, 2 + carrierTypeLength, carrierData, 0, carrierDataLength);
    }
    else {
      carrierData = NdefConstants.EMPTY_BYTE_ARRAY;
    }
    handoverCarrierRecord.setCarrierData(carrierData);

    return handoverCarrierRecord;
  }
View Full Code Here

TOP

Related Classes of org.nfctools.ndef.wkt.handover.records.HandoverCarrierRecord

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.