code: public OutputStream prepareForRecords(
  			OutputStream out, final SAMFormat format,
  			final SAMFileHeader header)
  		throws IOException {
  
          switch (format) {
              case SAM:
                  out = prepareSAMOrBAMStream(out, format, header);
                  break;
              case BAM:
                  out = prepareSAMOrBAMStream(out, format, header);
                  break;
              case CRAM:
                  out = prepareCRAMStream(out, format, header);
                  break;
              default:
                  throw new IllegalArgumentException
                      ("Unsupported SAM file format, must be one of SAM, BAM or CRAM");
          }
  
          // Important for BAM: if the caller doesn't want to use the new stream
          // for some reason, the BlockCompressedOutputStream's buffer would never
          // be flushed.
          out.flush();
          return out;
  	}
docs: Prepares the given output stream for writing of SAMRecords in the given
  format. This includes writing the given SAM header and, in the case of
  BAM or CRAM, writing some further metadata as well as compressing everything
  written. Returns a new stream to replace the original: it will do the
  appropriate compression for BAM/CRAM files.