finish method

void finish(
  1. int offset, [
  2. String? fileIdentifier
])

Finish off the creation of the buffer. The given offset is used as the root object offset, and usually references directly or indirectly every written object. If fileIdentifier is specified (and not null), it is interpreted as a 4-byte Latin-1 encoded string that should be placed at bytes 4-7 of the file.

Implementation

void finish(int offset, [String? fileIdentifier]) {
  final sizeBeforePadding = size();
  final requiredBytes = _sizeofUint32 * (fileIdentifier == null ? 1 : 2);
  _prepare(max(requiredBytes, _maxAlign), 1);
  final finishedSize = size();
  _setUint32AtTail(finishedSize, finishedSize - offset);
  if (fileIdentifier != null) {
    for (var i = 0; i < 4; i++) {
      _setUint8AtTail(
          finishedSize - _sizeofUint32 - i, fileIdentifier.codeUnitAt(i));
    }
  }

  // zero out the added padding
  for (var i = sizeBeforePadding + 1;
      i <= finishedSize - requiredBytes;
      i++) {
    _setUint8AtTail(i, 0);
  }
  _finished = true;
}