src/main/data/bam.js
function readAlignmentsToEnd(buffer: ArrayBuffer,
                             refName: string,
                             idxRange: ContigInterval<number>,
                             contained: boolean,
                             offset: VirtualOffset,
                             blocks: InflatedBlock[],
                             alignments: SamRead[]) {
  // We use jDataView and ArrayBuffer directly for a speedup over jBinary.
  // This parses reads ~2-3x faster than using ThinAlignment directly.
  var jv = new jDataView(buffer, 0, buffer.byteLength, true /* little endian */);
  var shouldAbort = false;
  var pos = 0;
  offset = offset.clone();
  var blockIndex = 0;
  try {
    while (pos < buffer.byteLength) {
      var readData = readAlignment(jv, pos, offset, refName);
      if (!readData) break;

      var {read, readLength} = readData;
      pos += readLength;
      if (isAlignmentInRange(read, idxRange, contained)) {
        alignments.push(read);
      }

      // Advance the VirtualOffset to reflect the new position
      offset.uoffset += readLength;
      var bufLen = blocks[blockIndex].buffer.byteLength;
      if (offset.uoffset >= bufLen) {
        offset.uoffset -= bufLen;
        offset.coffset += blocks[blockIndex].compressedLength;
        blockIndex++;
      }

      // Optimization: if the last alignment started after the requested range,
      // then no other chunks can possibly contain matching alignments.
      // TODO: use contigInterval.isAfterInterval when that's possible.
      var range = new ContigInterval(read.refID, read.pos, read.pos + 1);
      if (range.contig > idxRange.contig ||
          (range.contig == idxRange.contig && range.start() > idxRange.stop())) {
        shouldAbort = true;
        break;
      }
    }
    // Code gets here if the compression block ended exactly at the end of
    // an Alignment.
  } catch (e) {
    // Partial record
    if (!(e instanceof RangeError)) {
      throw e;
    }
  }

  return {
    shouldAbort,
    nextOffset: offset
  };
}
function readAlignmentsToEnd(buffer: ArrayBuffer,
                             refName: string,
                             idxRange: ContigInterval<number>,
                             contained: boolean,
                             offset: VirtualOffset,
                             blocks: InflatedBlock[],
                             alignments: SamRead[]) {
  // We use jDataView and ArrayBuffer directly for a speedup over jBinary.
  // This parses reads ~2-3x faster than using ThinAlignment directly.
  var jv = new jDataView(buffer, 0, buffer.byteLength, true /* little endian */);
  var shouldAbort = false;
  var pos = 0;
  offset = offset.clone();
  var blockIndex = 0;
  try {
    while (pos < buffer.byteLength) {
      var readData = readAlignment(jv, pos, offset, refName);
      if (!readData) break;

      var {read, readLength} = readData;
      pos += readLength;
      if (isAlignmentInRange(read, idxRange, contained)) {
        alignments.push(read);
      }

      // Advance the VirtualOffset to reflect the new position
      offset.uoffset += readLength;
      var bufLen = blocks[blockIndex].buffer.byteLength;
      if (offset.uoffset >= bufLen) {
        offset.uoffset -= bufLen;
        offset.coffset += blocks[blockIndex].compressedLength;
        blockIndex++;
      }

      // Optimization: if the last alignment started after the requested range,
      // then no other chunks can possibly contain matching alignments.
      // TODO: use contigInterval.isAfterInterval when that's possible.
      var range = new ContigInterval(read.refID, read.pos, read.pos + 1);
      if (range.contig > idxRange.contig ||
          (range.contig == idxRange.contig && range.start() > idxRange.stop())) {
        shouldAbort = true;
        break;
      }
    }
    // Code gets here if the compression block ended exactly at the end of
    // an Alignment.
  } catch (e) {
    // Partial record
    if (!(e instanceof RangeError)) {
      throw e;
    }
  }

  return {
    shouldAbort,
    nextOffset: offset
  };
}
This tracks how many bytes were read.