factoryOrNull = new SAMFileWriterFactory();
writer = factoryOrNull.makeSAMWriter(header, true, os);
} else {
SwapOutputStream sos = new SwapOutputStream();
final SAMTextWriter ret = new SAMTextWriter(sos);
ret.setSortOrder(header.getSortOrder(), true);
ret.setHeader(header);
ret.getWriter().flush();
writer = ret;
sos.delegate = os;
}