addByteData method

void addByteData(
  1. ByteData sourceData

Adds all the sourceData to the internal buffer. The data must conform to the internal format, i.e. the number of bytes must be sufficient to represent completely a certain number of elements, otherwise an exception will be thrown.


void addByteData(ByteData sourceData) {
  // Make sure that the data we receive is well formed.
  if (sourceData.lengthInBytes % elementSizeInBytes != 0) {
    throw Exception(
        'Provided number of bytes ${sourceData.lengthInBytes} not divisible by bytes per element $elementSizeInBytes.');

  // Allocate sufficient space for the new elements.
  final newElements = sourceData.lengthInBytes ~/ elementSizeInBytes;
  capacity = max(capacity, length + newElements);

  // Copy the data over.
  final targetByte = _elementNrToByteOffset(length);
  final sourceBytes = sourceData.buffer
      .asUint8List(sourceData.offsetInBytes, sourceData.lengthInBytes);
      targetByte, targetByte + sourceData.lengthInBytes, sourceBytes);

  // Update the number of elements.
  _elementsCount += newElements;