readChunkHeader function

XdfChunkHeader? readChunkHeader(
  1. Uint8List bytes,
  2. int position
)

Parse the chunk header at position of bytes, or null if bytes end before it does (at most 11 bytes are needed).

Implementation

XdfChunkHeader? readChunkHeader(Uint8List bytes, int position) {
  if (position >= bytes.length) return null;
  final r = XdfReader(bytes, position);
  try {
    final length = r.varLen();
    if (length < 2) throw FormatException('Invalid chunk length $length');
    final tag = r.uint16();
    return (tag: tag, contentOffset: r.position, contentLength: length - 2);
  } on FormatException catch (e) {
    if (e.message.startsWith('Unexpected end')) return null;
    rethrow;
  }
}