diff --git a/src/main/java/htsjdk/samtools/util/BlockCompressedOutputStream.java b/src/main/java/htsjdk/samtools/util/BlockCompressedOutputStream.java index 4e9a594875..30f325b0e5 100644 --- a/src/main/java/htsjdk/samtools/util/BlockCompressedOutputStream.java +++ b/src/main/java/htsjdk/samtools/util/BlockCompressedOutputStream.java @@ -111,6 +111,7 @@ public static DeflaterFactory getDefaultDeflaterFactory() { private final Deflater noCompressionDeflater = new Deflater(Deflater.NO_COMPRESSION, true); private final CRC32 crc32 = new CRC32(); private File file = null; + private String fileName = null; private long mBlockAddress = 0; @@ -161,6 +162,7 @@ public BlockCompressedOutputStream(final File file, final int compressionLevel) */ public BlockCompressedOutputStream(final File file, final int compressionLevel, final DeflaterFactory deflaterFactory) { this.file = file; + this.fileName = file.getAbsolutePath(); codec = new BinaryCodec(file, true); deflater = deflaterFactory.makeDeflater(compressionLevel, true); log.debug("Using deflater: " + deflater.getClass().getSimpleName()); @@ -196,7 +198,8 @@ public BlockCompressedOutputStream(final OutputStream os, final File file, final this.file = file; codec = new BinaryCodec(os); if (file != null) { - codec.setOutputFileName(file.getAbsolutePath()); + fileName = file.getAbsolutePath(); + codec.setOutputFileName(fileName); } deflater = deflaterFactory.makeDeflater(compressionLevel, true); log.debug("Using deflater: " + deflater.getClass().getSimpleName()); @@ -217,6 +220,19 @@ public static BlockCompressedOutputStream maybeBgzfWrapOutputStream(final File l } } + /** + * Sets the file name if it is not already set. + * @throws IllegalStateException if the file name was already set. + * @param fileName the filename. + */ + public void setFileName(final String fileName) { + if (this.fileName != null) { + throw new IllegalStateException("File name is already set: " + this.fileName); + } + this.fileName = fileName; + codec.setOutputFileName(fileName); + } + /** * Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) * is that it should have exactly the same effect as the call write(b, 0, b.length). diff --git a/src/main/java/htsjdk/tribble/index/tabix/TabixIndex.java b/src/main/java/htsjdk/tribble/index/tabix/TabixIndex.java index c0504ecc88..c4ba199a4d 100644 --- a/src/main/java/htsjdk/tribble/index/tabix/TabixIndex.java +++ b/src/main/java/htsjdk/tribble/index/tabix/TabixIndex.java @@ -207,8 +207,9 @@ public TabixFormat getFormatSpec() { */ @Override public void write(final Path tabixPath) throws IOException { - try(final LittleEndianOutputStream los = new LittleEndianOutputStream(new BlockCompressedOutputStream(Files.newOutputStream(tabixPath), null))) { - write(los); + try(final BlockCompressedOutputStream bcos = new BlockCompressedOutputStream(Files.newOutputStream(tabixPath), null)) { + bcos.setFileName(tabixPath.toUri().toString()); + write(new LittleEndianOutputStream(bcos)); } }