Program: 30515
Project: 30515
Buggy source:
/** 
 * Complete the current container and flush it to the output stream.
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws IOException
 */
protected void flushContainer() throws IllegalArgumentException, IllegalAccessException, IOException {
  final byte[] refs;
  String refSeqName=null;
  if (refSeqIndex == SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX)   refs=new byte[0];
 else {
    final SAMSequenceRecord sequence=samFileHeader.getSequence(refSeqIndex);
    refs=source.getReferenceBases(sequence,true);
    refSeqName=sequence.getSequenceName();
  }
  int start=SAMRecord.NO_ALIGNMENT_START;
  int stop=SAMRecord.NO_ALIGNMENT_START;
  for (  final SAMRecord r : samRecords) {
    if (r.getAlignmentStart() == SAMRecord.NO_ALIGNMENT_START)     continue;
    if (start == SAMRecord.NO_ALIGNMENT_START)     start=r.getAlignmentStart();
    start=Math.min(r.getAlignmentStart(),start);
    stop=Math.max(r.getAlignmentEnd(),stop);
  }
  ReferenceTracks tracks=null;
  if (preservation != null && preservation.areReferenceTracksRequired()) {
    tracks=new ReferenceTracks(refSeqIndex,refSeqName,refs);
    tracks.ensureRange(start,stop - start + 1);
    updateTracks(samRecords,tracks);
  }
  final List<CramCompressionRecord> cramRecords=new ArrayList<CramCompressionRecord>(samRecords.size());
  final Sam2CramRecordFactory sam2CramRecordFactory=new Sam2CramRecordFactory(refs,samFileHeader,cramVersion);
  sam2CramRecordFactory.preserveReadNames=preserveReadNames;
  sam2CramRecordFactory.captureAllTags=captureAllTags;
  sam2CramRecordFactory.captureTags.addAll(captureTags);
  sam2CramRecordFactory.ignoreTags.addAll(ignoreTags);
  containerFactory.setPreserveReadNames(preserveReadNames);
  int index=0;
  int prevAlStart=start;
  for (  final SAMRecord samRecord : samRecords) {
    final CramCompressionRecord cramRecord=sam2CramRecordFactory.createCramRecord(samRecord);
    cramRecord.index=++index;
    cramRecord.alignmentDelta=samRecord.getAlignmentStart() - prevAlStart;
    cramRecord.alignmentStart=samRecord.getAlignmentStart();
    prevAlStart=samRecord.getAlignmentStart();
    cramRecords.add(cramRecord);
    if (preservation != null)     preservation.addQualityScores(samRecord,cramRecord,tracks);
 else     if (cramRecord.qualityScores != SAMRecord.NULL_QUALS)     cramRecord.setForcePreserveQualityScores(true);
  }
  if (sam2CramRecordFactory.getBaseCount() < 3 * sam2CramRecordFactory.getFeatureCount())   log.warn("String_Node_Str");
{
    if (samFileHeader.getSortOrder() == SAMFileHeader.SortOrder.coordinate) {
      final Map<String,CramCompressionRecord> primaryMateMap=new TreeMap<String,CramCompressionRecord>();
      final Map<String,CramCompressionRecord> secondaryMateMap=new TreeMap<String,CramCompressionRecord>();
      for (      final CramCompressionRecord r : cramRecords) {
        if (!r.isMultiFragment()) {
          r.setDetached(true);
          r.setHasMateDownStream(false);
          r.recordsToNextFragment=-1;
          r.next=null;
          r.previous=null;
        }
 else {
          final String name=r.readName;
          final Map<String,CramCompressionRecord> mateMap=r.isSecondaryAlignment() ? secondaryMateMap : primaryMateMap;
          final CramCompressionRecord mate=mateMap.get(name);
          if (mate == null) {
            mateMap.put(name,r);
          }
 else {
            CramCompressionRecord prev=mate;
            while (prev.next != null)             prev=prev.next;
            prev.recordsToNextFragment=r.index - prev.index - 1;
            prev.next=r;
            r.previous=prev;
            r.previous.setHasMateDownStream(true);
            r.setHasMateDownStream(false);
            r.setDetached(false);
            r.previous.setDetached(false);
          }
        }
      }
      for (      final CramCompressionRecord cramRecord : cramRecords) {
        if (cramRecord.next == null || cramRecord.previous != null)         continue;
        CramCompressionRecord last=cramRecord;
        while (last.next != null)         last=last.next;
        if (cramRecord.isFirstSegment() && last.isLastSegment()) {
          final int templateLength=CramNormalizer.computeInsertSize(cramRecord,last);
          if (cramRecord.templateSize == templateLength) {
            last=cramRecord.next;
            while (last.next != null) {
              if (last.templateSize != -templateLength)               break;
              last=last.next;
            }
            if (last.templateSize != -templateLength)             detach(cramRecord);
          }
        }
 else         detach(cramRecord);
      }
      for (      final CramCompressionRecord cramRecord : primaryMateMap.values()) {
        if (cramRecord.next != null)         continue;
        cramRecord.setDetached(true);
        cramRecord.setHasMateDownStream(false);
        cramRecord.recordsToNextFragment=-1;
        cramRecord.next=null;
        cramRecord.previous=null;
      }
      for (      final CramCompressionRecord cramRecord : secondaryMateMap.values()) {
        if (cramRecord.next != null)         continue;
        cramRecord.setDetached(true);
        cramRecord.setHasMateDownStream(false);
        cramRecord.recordsToNextFragment=-1;
        cramRecord.next=null;
        cramRecord.previous=null;
      }
    }
 else {
      for (      final CramCompressionRecord cramRecord : cramRecords) {
        cramRecord.setDetached(true);
      }
    }
  }
{
    @SuppressWarnings("String_Node_Str") boolean assertsEnabled=false;
    assert assertsEnabled=true;
    if (assertsEnabled) {
      final Cram2SamRecordFactory f=new Cram2SamRecordFactory(samFileHeader);
      for (int i=0; i < samRecords.size(); i++) {
        final SAMRecord restoredSamRecord=f.create(cramRecords.get(i));
        assert(restoredSamRecord.getAlignmentStart() == samRecords.get(i).getAlignmentStart());
        assert(restoredSamRecord.getReferenceName().equals(samRecords.get(i).getReferenceName()));
        assert(restoredSamRecord.getReadString().equals(samRecords.get(i).getReadString()));
        assert(restoredSamRecord.getBaseQualityString().equals(samRecords.get(i).getBaseQualityString()));
      }
    }
  }
  final Container container=containerFactory.buildContainer(cramRecords);
  for (  final Slice slice : container.slices)   slice.setRefMD5(refs);
  container.offset=offset;
  offset+=ContainerIO.writeContainer(cramVersion,container,outputStream);
  if (indexer != null) {
    for (    final Slice slice : container.slices) {
      indexer.processAlignment(slice);
    }
  }
  samRecords.clear();
}

Fixed version (ground truth):
/** 
 * Complete the current container and flush it to the output stream.
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws IOException
 */
protected void flushContainer() throws IllegalArgumentException, IllegalAccessException, IOException {
  final byte[] refs;
  String refSeqName=null;
  if (refSeqIndex == SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX)   refs=new byte[0];
 else {
    final SAMSequenceRecord sequence=samFileHeader.getSequence(refSeqIndex);
    refs=source.getReferenceBases(sequence,true);
    refSeqName=sequence.getSequenceName();
  }
  int start=SAMRecord.NO_ALIGNMENT_START;
  int stop=SAMRecord.NO_ALIGNMENT_START;
  for (  final SAMRecord r : samRecords) {
    if (r.getAlignmentStart() == SAMRecord.NO_ALIGNMENT_START)     continue;
    if (start == SAMRecord.NO_ALIGNMENT_START)     start=r.getAlignmentStart();
    start=Math.min(r.getAlignmentStart(),start);
    stop=Math.max(r.getAlignmentEnd(),stop);
  }
  ReferenceTracks tracks=null;
  if (preservation != null && preservation.areReferenceTracksRequired()) {
    tracks=new ReferenceTracks(refSeqIndex,refSeqName,refs);
    tracks.ensureRange(start,stop - start + 1);
    updateTracks(samRecords,tracks);
  }
  final List<CramCompressionRecord> cramRecords=new ArrayList<CramCompressionRecord>(samRecords.size());
  final Sam2CramRecordFactory sam2CramRecordFactory=new Sam2CramRecordFactory(refs,samFileHeader,cramVersion);
  sam2CramRecordFactory.preserveReadNames=preserveReadNames;
  sam2CramRecordFactory.captureAllTags=captureAllTags;
  sam2CramRecordFactory.captureTags.addAll(captureTags);
  sam2CramRecordFactory.ignoreTags.addAll(ignoreTags);
  containerFactory.setPreserveReadNames(preserveReadNames);
  int index=0;
  int prevAlStart=start;
  for (  final SAMRecord samRecord : samRecords) {
    final CramCompressionRecord cramRecord=sam2CramRecordFactory.createCramRecord(samRecord);
    cramRecord.index=++index;
    cramRecord.alignmentDelta=samRecord.getAlignmentStart() - prevAlStart;
    cramRecord.alignmentStart=samRecord.getAlignmentStart();
    prevAlStart=samRecord.getAlignmentStart();
    cramRecords.add(cramRecord);
    if (preservation != null)     preservation.addQualityScores(samRecord,cramRecord,tracks);
 else     if (cramRecord.qualityScores != SAMRecord.NULL_QUALS)     cramRecord.setForcePreserveQualityScores(true);
  }
  if (sam2CramRecordFactory.getBaseCount() < 3 * sam2CramRecordFactory.getFeatureCount())   log.warn("String_Node_Str");
{
    if (samFileHeader.getSortOrder() == SAMFileHeader.SortOrder.coordinate) {
      final Map<String,CramCompressionRecord> primaryMateMap=new TreeMap<String,CramCompressionRecord>();
      final Map<String,CramCompressionRecord> secondaryMateMap=new TreeMap<String,CramCompressionRecord>();
      for (      final CramCompressionRecord r : cramRecords) {
        if (!r.isMultiFragment()) {
          r.setDetached(true);
          r.setHasMateDownStream(false);
          r.recordsToNextFragment=-1;
          r.next=null;
          r.previous=null;
        }
 else {
          final String name=r.readName;
          final Map<String,CramCompressionRecord> mateMap=r.isSecondaryAlignment() ? secondaryMateMap : primaryMateMap;
          final CramCompressionRecord mate=mateMap.get(name);
          if (mate == null) {
            mateMap.put(name,r);
          }
 else {
            CramCompressionRecord prev=mate;
            while (prev.next != null)             prev=prev.next;
            prev.recordsToNextFragment=r.index - prev.index - 1;
            prev.next=r;
            r.previous=prev;
            r.previous.setHasMateDownStream(true);
            r.setHasMateDownStream(false);
            r.setDetached(false);
            r.previous.setDetached(false);
          }
        }
      }
      for (      final CramCompressionRecord cramRecord : cramRecords) {
        if (cramRecord.next == null || cramRecord.previous != null)         continue;
        CramCompressionRecord last=cramRecord;
        while (last.next != null)         last=last.next;
        if (cramRecord.isFirstSegment() && last.isLastSegment()) {
          final int templateLength=CramNormalizer.computeInsertSize(cramRecord,last);
          if (cramRecord.templateSize == templateLength) {
            last=cramRecord.next;
            while (last.next != null) {
              if (last.templateSize != -templateLength)               break;
              last=last.next;
            }
            if (last.templateSize != -templateLength)             detach(cramRecord);
          }
 else           detach(cramRecord);
        }
 else         detach(cramRecord);
      }
      for (      final CramCompressionRecord cramRecord : primaryMateMap.values()) {
        if (cramRecord.next != null)         continue;
        cramRecord.setDetached(true);
        cramRecord.setHasMateDownStream(false);
        cramRecord.recordsToNextFragment=-1;
        cramRecord.next=null;
        cramRecord.previous=null;
      }
      for (      final CramCompressionRecord cramRecord : secondaryMateMap.values()) {
        if (cramRecord.next != null)         continue;
        cramRecord.setDetached(true);
        cramRecord.setHasMateDownStream(false);
        cramRecord.recordsToNextFragment=-1;
        cramRecord.next=null;
        cramRecord.previous=null;
      }
    }
 else {
      for (      final CramCompressionRecord cramRecord : cramRecords) {
        cramRecord.setDetached(true);
      }
    }
  }
{
    @SuppressWarnings("String_Node_Str") boolean assertsEnabled=false;
    assert assertsEnabled=true;
    if (assertsEnabled) {
      final Cram2SamRecordFactory f=new Cram2SamRecordFactory(samFileHeader);
      for (int i=0; i < samRecords.size(); i++) {
        final SAMRecord restoredSamRecord=f.create(cramRecords.get(i));
        assert(restoredSamRecord.getAlignmentStart() == samRecords.get(i).getAlignmentStart());
        assert(restoredSamRecord.getReferenceName().equals(samRecords.get(i).getReferenceName()));
        assert(restoredSamRecord.getReadString().equals(samRecords.get(i).getReadString()));
        assert(restoredSamRecord.getBaseQualityString().equals(samRecords.get(i).getBaseQualityString()));
      }
    }
  }
  final Container container=containerFactory.buildContainer(cramRecords);
  for (  final Slice slice : container.slices)   slice.setRefMD5(refs);
  container.offset=offset;
  offset+=ContainerIO.writeContainer(cramVersion,container,outputStream);
  if (indexer != null) {
    for (    final Slice slice : container.slices) {
      indexer.processAlignment(slice);
    }
  }
  samRecords.clear();
}