writeTypeValue method

void writeTypeValue(
  1. int majorType,
  2. int value
)

Implementation

void writeTypeValue(int majorType, int value) {
  var type = majorType;
  type <<= cbor.majorTypeShift;
  if (value < cbor.ai24) {
    // Value
    _out.putByte(type | value);
  } else if (value < cbor.two8) {
    // Uint8
    _out.putByte(type | cbor.ai24);
    _out.putByte(value);
  } else if (value < cbor.two16) {
    // Uint16
    _out.putByte(type | cbor.ai25);
    final buff = Uint16Buffer(1);
    buff[0] = value;
    final ulist = Uint8List.view(buff.buffer);
    final data = Uint8Buffer();
    data.addAll(ulist.toList().reversed);
    _out.putBytes(data);
  } else if (value < cbor.two32) {
    // Uint32
    _out.putByte(type | cbor.ai26);
    final buff = Uint32Buffer(1);
    buff[0] = value;
    final ulist = Uint8List.view(buff.buffer);
    final data = Uint8Buffer();
    data.addAll(ulist.toList().reversed);
    _out.putBytes(data);
  } else {
    // Encode to a bignum, if the value can be represented as
    // an integer it must be greater than 2*32 so encode as 64 bit.
    final bignum = BigInt.from(value);
    if (kIsWeb) {
      var data = serializeValue(0, 27, bignum.toRadixString(16));
      var buf = Uint8Buffer();
      buf.addAll(data.asUint8List());
      addBuilderOutput(buf);
    } else if (bignum.isValidInt) {
      // Uint64
      _out.putByte(type | cbor.ai27);
      final buff = Uint64Buffer(1);
      buff[0] = value;
      final ulist = Uint8List.view(buff.buffer);
      final data = Uint8Buffer();
      data.addAll(ulist.toList().reversed);
      _out.putBytes(data);
    } else {
      // Bignum - encoded as a tag value
      writeBignum(BigInt.from(value));
    }
  }
}