addBytes method

Message addBytes(
  1. Uint8List array, [
  2. bool includeLength = true
])

Adds a Uint8List to the message.

array : The array to add. includeLength : Whether or not to include the length of the array in the message.

Returns the message that the array was added to.

Implementation

Message addBytes(Uint8List array, [bool includeLength = true]) {
  if (includeLength) {
    addVarULong(array.length);
  }

  int writeAmount = array.length * _bitsPerByte;
  if (unwrittenBits < writeAmount) {
    throw InsufficientCapacityException.withArrayDetails(
        this, array.length, byteName, _bitsPerByte);
  }

  if (_writeBit % _bitsPerByte == 0) {
    int bit = _writeBit % _bitsPerSegment;
    if (bit + writeAmount > _bitsPerSegment) {
      // Range reaches into subsequent segment(s)
      data[(_writeBit + writeAmount) ~/ _bitsPerSegment] = 0;
    } else if (bit == 0) {
      // Range doesn't fill the current segment, but begins the segment
      data[_writeBit ~/ _bitsPerSegment] = 0;
    }

    Helper.blockCopy(array, 0, data, _writeBit ~/ _bitsPerByte, array.length);
    _writeBit += writeAmount;
  } else {
    for (int i = 0; i < array.length; i++) {
      Converter.byteToBitsFromUlongs(array[i], data, _writeBit);
      _writeBit += _bitsPerByte;
    }
  }
  return this;
}