decodeStream static method

NDEFRecord decodeStream(
  1. ByteStream stream,
  2. TypeFactory typeFactory
)

Decode a NDEF NDEFRecord from part of ByteStream.

Implementation

static NDEFRecord decodeStream(ByteStream stream, TypeFactory typeFactory) {
  var flags = NDEFRecordFlags(data: stream.readByte());

  int typeLength = stream.readByte();
  int payloadLength;
  int idLength = 0;
  if (flags.SR) {
    payloadLength = stream.readByte();
  } else {
    payloadLength = stream.readInt(4);
  }
  if (flags.IL) {
    idLength = stream.readByte();
  }

  if ([0, 5, 6].contains(flags.TNF)) {
    assert(typeLength == 0, "TYPE_LENGTH must be 0 when TNF is 0,5,6");
  }
  if (flags.TNF == 0) {
    assert(idLength == 0, "ID_LENGTH must be 0 when TNF is 0");
    assert(payloadLength == 0, "PAYLOAD_LENGTH must be 0 when TNF is 0");
  }
  if ([1, 2, 3, 4].contains(flags.TNF)) {
    assert(typeLength > 0, "TYPE_LENGTH must be > 0 when TNF is 1,2,3,4");
  }

  var type = stream.readBytes(typeLength);

  Uint8List? id;
  if (idLength != 0) {
    id = stream.readBytes(idLength);
  }

  var payload = stream.readBytes(payloadLength);
  var typeNameFormat = TypeNameFormat.values[flags.TNF];

  var decoded = doDecode(typeNameFormat, type, payload,
      id: id, typeFactory: typeFactory);
  decoded.flags = flags;
  return decoded;
}