close method

void close(
  1. void onFrame(
    1. FrameBorrowedView frame
    ), {
  2. bool allowTrailingBytesAtEof = false,
})

Finish decoding at EOF.

  • Drains all remaining complete frames via tryDecodeBorrowed.
  • If the codec mixes in FinalizableCodec, calls tryFinalize repeatedly to flush any final frame (wrapped as owned views).
  • If trailing bytes remain and allowTrailingBytesAtEof is false, throws FormatException.

Implementation

void close(
  void Function(FrameBorrowedView frame) onFrame, {
  bool allowTrailingBytesAtEof = false,
}) {
  _drain(onFrame);

  // Let the codec finalize (emit trailing frame at EOF).
  if (codec is FinalizableCodec<Uint8List>) {
    final c = codec as FinalizableCodec<Uint8List>;
    while (true) {
      final bytes = c.tryFinalize(_rb);
      if (bytes == null) break;
      onFrame(FrameBorrowedView.owned(bytes));
    }
  }

  if (!allowTrailingBytesAtEof && (_rb.length != 0 || codec.hasPending)) {
    final pendingNote = codec.hasPending ? ' (codec has pending state)' : '';
    throw FormatException(
      'Truncated frame at EOF: ${_rb.length} trailing bytes buffered'
      '$pendingNote.',
    );
  }
}