encodeContent method

Uint8List encodeContent()

Implementation

Uint8List encodeContent() {
  if (objectIdentifier.length < 2) {
    throw Exception('Object identifier sould have atleast 2 subcomponents');
  }

  if (objectIdentifier[0] >= 3) {
    throw Exception(
        'First subcomponent of Object identifier should be less that 3');
  }

  if (objectIdentifier[1] >= 40) {
    throw Exception(
        'Second subcomponent of Object identifier should be less that 40');
  }

  final first = objectIdentifier[0] * 40 + objectIdentifier[1];

  int length = objectIdentifier.skip(2).fold(
      0,
      (previousValue, element) =>
          previousValue +
          (element == 0 ? 1 : (element.bitLength / 7).ceil()));
  length += first == 0 ? 1 : (first.bitLength / 7).ceil();

  final ret = Uint8List(length);
  int offset = 0;
  offset += _encodeSubIdentifier(first, ret.buffer.asByteData(offset));

  for (int element in objectIdentifier.skip(2)) {
    offset += _encodeSubIdentifier(element, ret.buffer.asByteData(offset));
  }

  if (offset != length) {
    throw Exception('error in implementation');
  }

  return ret;
}