Program: 30449
Project: 30449
Buggy source:
@Override public SamReader open(final SamInputResource resource){
  final SamReader.PrimitiveSamReader primitiveSamReader;
  try {
    final InputResource data=resource.data();
    final InputResource indexMaybe=resource.indexMaybe();
    final boolean indexDefined=indexMaybe != null;
    final InputResource.Type type=data.type();
    if (type == InputResource.Type.URL) {
      SamReader reader=customReaderFactory.maybeOpen(data.asUrl());
      if (reader != null) {
        return reader;
      }
    }
    if (type == InputResource.Type.SEEKABLE_STREAM || type == InputResource.Type.URL) {
      if (SamStreams.sourceLikeBam(data.asUnbufferedSeekableStream())) {
        final SeekableStream bufferedIndexStream;
        if (indexDefined && indexMaybe.asUnbufferedSeekableStream() != null) {
          bufferedIndexStream=IOUtil.maybeBufferedSeekableStream(indexMaybe.asUnbufferedSeekableStream());
        }
 else {
          bufferedIndexStream=null;
        }
        primitiveSamReader=new BAMFileReader(IOUtil.maybeBufferedSeekableStream(data.asUnbufferedSeekableStream()),bufferedIndexStream,false,asynchronousIO,validationStringency,this.samRecordFactory);
      }
 else       if (SamStreams.sourceLikeCram(data.asUnbufferedSeekableStream())) {
        if (referenceSource == null) {
          referenceSource=ReferenceSource.getDefaultCRAMReferenceSource();
        }
        SeekableStream bufferedIndexStream=indexDefined ? IOUtil.maybeBufferedSeekableStream(indexMaybe.asUnbufferedSeekableStream()) : null;
        primitiveSamReader=new CRAMFileReader(IOUtil.maybeBufferedSeekableStream(data.asUnbufferedSeekableStream()),bufferedIndexStream,referenceSource,validationStringency);
      }
 else {
        LOG.warn("String_Node_Str");
        primitiveSamReader=new SAMTextReader(IOUtil.toBufferedStream(data.asUnbufferedInputStream()),validationStringency,this.samRecordFactory);
      }
    }
 else     if (type == InputResource.Type.SRA_ACCESSION) {
      primitiveSamReader=new SRAFileReader(data.asSRAAccession());
    }
 else {
      InputStream bufferedStream=IOUtil.maybeBufferInputStream(data.asUnbufferedInputStream(),Math.max(Defaults.BUFFER_SIZE,BlockCompressedStreamConstants.MAX_COMPRESSED_BLOCK_SIZE));
      File sourceFile=data.asFile();
      final File indexFile=indexMaybe == null ? null : indexMaybe.asFile();
      if (SamStreams.isBAMFile(bufferedStream)) {
        if (sourceFile == null || !sourceFile.isFile()) {
          final SeekableStream indexSeekable=indexMaybe == null ? null : indexMaybe.asUnbufferedSeekableStream();
          SeekableStream sourceSeekable=data.asUnbufferedSeekableStream();
          if (indexFile != null || null == sourceSeekable || null == indexSeekable) {
            primitiveSamReader=new BAMFileReader(bufferedStream,indexFile,false,asynchronousIO,validationStringency,this.samRecordFactory);
          }
 else {
            sourceSeekable.seek(0);
            primitiveSamReader=new BAMFileReader(sourceSeekable,indexSeekable,false,asynchronousIO,validationStringency,this.samRecordFactory);
          }
        }
 else {
          bufferedStream.close();
          primitiveSamReader=new BAMFileReader(sourceFile,indexFile,false,asynchronousIO,validationStringency,this.samRecordFactory);
        }
      }
 else       if (BlockCompressedInputStream.isValidFile(bufferedStream)) {
        primitiveSamReader=new SAMTextReader(new BlockCompressedInputStream(bufferedStream),validationStringency,this.samRecordFactory);
      }
 else       if (SamStreams.isGzippedSAMFile(bufferedStream)) {
        primitiveSamReader=new SAMTextReader(new GZIPInputStream(bufferedStream),validationStringency,this.samRecordFactory);
      }
 else       if (SamStreams.isCRAMFile(bufferedStream)) {
        if (referenceSource == null) {
          referenceSource=ReferenceSource.getDefaultCRAMReferenceSource();
        }
        if (sourceFile == null || !sourceFile.isFile()) {
          primitiveSamReader=new CRAMFileReader(bufferedStream,indexFile,referenceSource,validationStringency);
        }
 else {
          bufferedStream.close();
          primitiveSamReader=new CRAMFileReader(sourceFile,indexFile,referenceSource,validationStringency);
        }
      }
 else       if (sourceFile != null && isSra(sourceFile)) {
        if (bufferedStream != null) {
          bufferedStream.close();
        }
        primitiveSamReader=new SRAFileReader(new SRAAccession(sourceFile.getPath()));
      }
 else {
        if (indexDefined) {
          bufferedStream.close();
          throw new RuntimeException("String_Node_Str");
        }
        primitiveSamReader=new SAMTextReader(bufferedStream,sourceFile,validationStringency,this.samRecordFactory);
      }
    }
    final SamReader.PrimitiveSamReaderToSamReaderAdapter reader=new SamReader.PrimitiveSamReaderToSamReaderAdapter(primitiveSamReader,resource);
    for (    final Option option : enabledOptions) {
      option.applyTo(reader);
    }
    return reader;
  }
 catch (  final IOException e) {
    throw new RuntimeIOException(e);
  }
}

Fixed version (ground truth):
@Override public SamReader open(final SamInputResource resource){
  final SamReader.PrimitiveSamReader primitiveSamReader;
  try {
    final InputResource data=resource.data();
    final InputResource indexMaybe=resource.indexMaybe();
    final boolean indexDefined=indexMaybe != null;
    final InputResource.Type type=data.type();
    if (type == InputResource.Type.URL) {
      SamReader reader=customReaderFactory.maybeOpen(data.asUrl());
      if (reader != null) {
        return reader;
      }
    }
    if (type == InputResource.Type.SEEKABLE_STREAM || type == InputResource.Type.URL) {
      if (SamStreams.sourceLikeBam(data.asUnbufferedSeekableStream())) {
        final SeekableStream bufferedIndexStream;
        if (indexDefined && indexMaybe.asUnbufferedSeekableStream() != null) {
          bufferedIndexStream=IOUtil.maybeBufferedSeekableStream(indexMaybe.asUnbufferedSeekableStream());
        }
 else {
          bufferedIndexStream=null;
        }
        primitiveSamReader=new BAMFileReader(IOUtil.maybeBufferedSeekableStream(data.asUnbufferedSeekableStream()),bufferedIndexStream,false,asynchronousIO,validationStringency,this.samRecordFactory);
      }
 else       if (SamStreams.sourceLikeCram(data.asUnbufferedSeekableStream())) {
        if (referenceSource == null) {
          referenceSource=ReferenceSource.getDefaultCRAMReferenceSource();
        }
        SeekableStream bufferedIndexStream=indexDefined ? IOUtil.maybeBufferedSeekableStream(indexMaybe.asUnbufferedSeekableStream()) : null;
        primitiveSamReader=new CRAMFileReader(IOUtil.maybeBufferedSeekableStream(data.asUnbufferedSeekableStream()),bufferedIndexStream,referenceSource,validationStringency);
      }
 else {
        LOG.warn("String_Node_Str");
        primitiveSamReader=new SAMTextReader(IOUtil.toBufferedStream(data.asUnbufferedInputStream()),validationStringency,this.samRecordFactory);
      }
    }
 else     if (type == InputResource.Type.SRA_ACCESSION) {
      primitiveSamReader=new SRAFileReader(data.asSRAAccession());
    }
 else {
      InputStream bufferedStream=IOUtil.maybeBufferInputStream(data.asUnbufferedInputStream(),Math.max(Defaults.BUFFER_SIZE,BlockCompressedStreamConstants.MAX_COMPRESSED_BLOCK_SIZE));
      File sourceFile=data.asFile();
      final File indexFile=indexMaybe == null ? null : indexMaybe.asFile();
      if (SamStreams.isBAMFile(bufferedStream)) {
        if (sourceFile == null || !sourceFile.isFile()) {
          final SeekableStream indexSeekable=indexMaybe == null ? null : indexMaybe.asUnbufferedSeekableStream();
          SeekableStream sourceSeekable=data.asUnbufferedSeekableStream();
          if (null == sourceSeekable || null == indexSeekable) {
            primitiveSamReader=new BAMFileReader(bufferedStream,indexFile,false,asynchronousIO,validationStringency,this.samRecordFactory);
          }
 else {
            sourceSeekable.seek(0);
            primitiveSamReader=new BAMFileReader(sourceSeekable,indexSeekable,false,asynchronousIO,validationStringency,this.samRecordFactory);
          }
        }
 else {
          bufferedStream.close();
          primitiveSamReader=new BAMFileReader(sourceFile,indexFile,false,asynchronousIO,validationStringency,this.samRecordFactory);
        }
      }
 else       if (BlockCompressedInputStream.isValidFile(bufferedStream)) {
        primitiveSamReader=new SAMTextReader(new BlockCompressedInputStream(bufferedStream),validationStringency,this.samRecordFactory);
      }
 else       if (SamStreams.isGzippedSAMFile(bufferedStream)) {
        primitiveSamReader=new SAMTextReader(new GZIPInputStream(bufferedStream),validationStringency,this.samRecordFactory);
      }
 else       if (SamStreams.isCRAMFile(bufferedStream)) {
        if (referenceSource == null) {
          referenceSource=ReferenceSource.getDefaultCRAMReferenceSource();
        }
        if (sourceFile == null || !sourceFile.isFile()) {
          primitiveSamReader=new CRAMFileReader(bufferedStream,indexFile,referenceSource,validationStringency);
        }
 else {
          bufferedStream.close();
          primitiveSamReader=new CRAMFileReader(sourceFile,indexFile,referenceSource,validationStringency);
        }
      }
 else       if (sourceFile != null && isSra(sourceFile)) {
        if (bufferedStream != null) {
          bufferedStream.close();
        }
        primitiveSamReader=new SRAFileReader(new SRAAccession(sourceFile.getPath()));
      }
 else {
        if (indexDefined) {
          bufferedStream.close();
          throw new RuntimeException("String_Node_Str");
        }
        primitiveSamReader=new SAMTextReader(bufferedStream,sourceFile,validationStringency,this.samRecordFactory);
      }
    }
    final SamReader.PrimitiveSamReaderToSamReaderAdapter reader=new SamReader.PrimitiveSamReaderToSamReaderAdapter(primitiveSamReader,resource);
    for (    final Option option : enabledOptions) {
      option.applyTo(reader);
    }
    return reader;
  }
 catch (  final IOException e) {
    throw new RuntimeIOException(e);
  }
}