Package htsjdk.samtools.util
Class BlockCompressedOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- htsjdk.samtools.util.BlockCompressedOutputStream
-
- All Implemented Interfaces:
LocationAware
,Closeable
,Flushable
,AutoCloseable
- Direct Known Subclasses:
TerminatorlessBlockCompressedOutputStream
public class BlockCompressedOutputStream extends OutputStream implements LocationAware
Writer for a file that is a series of gzip blocks (BGZF format). The caller just treats it as an OutputStream, and under the covers a gzip block is written when the amount of uncompressed as-yet-unwritten bytes reaches a threshold. The advantage of BGZF over conventional gzip is that BGZF allows for seeking without having to scan through the entire file up to the position being sought. Note that the flush() method should not be called by client unless you know what you're doing, because it forces a gzip block to be written even if the number of buffered bytes has not reached threshold. close(), on the other hand, must be called when done writing in order to force the last gzip block to be written. c.f. http://samtools.sourceforge.net/SAM1.pdf for details of BGZF file format.
-
-
Constructor Summary
Constructors Constructor Description BlockCompressedOutputStream(File file)
Uses default compression level, which is 5 unless changed by setCompressionLevel Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
.BlockCompressedOutputStream(File file, int compressionLevel)
Prepare to compress at the given compression levelBlockCompressedOutputStream(File file, int compressionLevel, DeflaterFactory deflaterFactory)
Prepare to compress at the given compression levelBlockCompressedOutputStream(OutputStream os, File file)
Uses default compression level, which is 5 unless changed by setCompressionLevel Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
.BlockCompressedOutputStream(OutputStream os, File file, int compressionLevel)
Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
.BlockCompressedOutputStream(OutputStream os, File file, int compressionLevel, DeflaterFactory deflaterFactory)
Creates the output stream.BlockCompressedOutputStream(OutputStream os, Path file)
Uses default compression level, which is 5 unless changed by setCompressionLevel Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
.BlockCompressedOutputStream(OutputStream os, Path file, int compressionLevel)
Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
.BlockCompressedOutputStream(OutputStream os, Path file, int compressionLevel, DeflaterFactory deflaterFactory)
Creates the output stream.BlockCompressedOutputStream(String filename)
Uses default compression level, which is 5 unless changed by setCompressionLevel Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
.BlockCompressedOutputStream(String filename, int compressionLevel)
Prepare to compress at the given compression level Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
.BlockCompressedOutputStream(Path path, int compressionLevel, DeflaterFactory deflaterFactory)
Prepare to compress at the given compression level
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addIndexer(OutputStream outputStream)
Adds a GZIIndexer to the block compressed output stream to be written to the specified output stream.void
close()
close() must be called in order to flush any remaining buffered bytes.void
close(boolean writeTerminatorBlock)
void
flush()
WARNING: flush() affects the output format, because it causes the current contents of uncompressedBuffer to be compressed and written, even if it isn't full.static int
getDefaultCompressionLevel()
static DeflaterFactory
getDefaultDeflaterFactory()
long
getFilePointer()
Encode virtual file pointer Upper 48 bits is the byte offset into the compressed stream of a block.long
getPosition()
The current offset, in bytes, of this stream/writer/file.static BlockCompressedOutputStream
maybeBgzfWrapOutputStream(File location, OutputStream output)
static void
setDefaultCompressionLevel(int compressionLevel)
Sets the GZip compression level for subsequent BlockCompressedOutputStream object creation that do not specify the compression level.static void
setDefaultDeflaterFactory(DeflaterFactory deflaterFactory)
Sets the defaultDeflaterFactory
that will be used for all instances unless specified otherwise in the constructor.void
write(byte[] bytes)
Writes b.length bytes from the specified byte array to this output stream.void
write(byte[] bytes, int startIndex, int numBytes)
Writes len bytes from the specified byte array starting at offset off to this output stream.void
write(int b)
-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Constructor Detail
-
BlockCompressedOutputStream
public BlockCompressedOutputStream(String filename)
Uses default compression level, which is 5 unless changed by setCompressionLevel Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
. UseBlockCompressedOutputStream(File, int, DeflaterFactory)
to specify a custom factory.
-
BlockCompressedOutputStream
public BlockCompressedOutputStream(File file)
Uses default compression level, which is 5 unless changed by setCompressionLevel Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
. UseBlockCompressedOutputStream(File, int, DeflaterFactory)
to specify a custom factory.
-
BlockCompressedOutputStream
public BlockCompressedOutputStream(String filename, int compressionLevel)
Prepare to compress at the given compression level Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
.- Parameters:
compressionLevel
- 1 <= compressionLevel <= 9
-
BlockCompressedOutputStream
public BlockCompressedOutputStream(File file, int compressionLevel)
Prepare to compress at the given compression level- Parameters:
compressionLevel
- 1 <= compressionLevel <= 9 Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
. UseBlockCompressedOutputStream(File, int, DeflaterFactory)
to specify a custom factory.
-
BlockCompressedOutputStream
public BlockCompressedOutputStream(File file, int compressionLevel, DeflaterFactory deflaterFactory)
Prepare to compress at the given compression level- Parameters:
compressionLevel
- 1 <= compressionLevel <= 9deflaterFactory
- custom factory to create deflaters (overrides the default)
-
BlockCompressedOutputStream
public BlockCompressedOutputStream(Path path, int compressionLevel, DeflaterFactory deflaterFactory)
Prepare to compress at the given compression level- Parameters:
compressionLevel
- 1 <= compressionLevel <= 9deflaterFactory
- custom factory to create deflaters (overrides the default)
-
BlockCompressedOutputStream
public BlockCompressedOutputStream(OutputStream os, File file)
Uses default compression level, which is 5 unless changed by setCompressionLevel Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
. UseBlockCompressedOutputStream(OutputStream, File, int, DeflaterFactory)
to specify a custom factory.- Parameters:
file
- may be null
-
BlockCompressedOutputStream
public BlockCompressedOutputStream(OutputStream os, Path file)
Uses default compression level, which is 5 unless changed by setCompressionLevel Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
. UseBlockCompressedOutputStream(OutputStream, File, int, DeflaterFactory)
to specify a custom factory.- Parameters:
file
- may be null
-
BlockCompressedOutputStream
public BlockCompressedOutputStream(OutputStream os, File file, int compressionLevel)
Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
. UseBlockCompressedOutputStream(OutputStream, File, int, DeflaterFactory)
to specify a custom factory.
-
BlockCompressedOutputStream
public BlockCompressedOutputStream(OutputStream os, Path file, int compressionLevel)
Note: this constructor uses the defaultDeflaterFactory
, seegetDefaultDeflaterFactory()
. UseBlockCompressedOutputStream(OutputStream, File, int, DeflaterFactory)
to specify a custom factory.
-
BlockCompressedOutputStream
public BlockCompressedOutputStream(OutputStream os, File file, int compressionLevel, DeflaterFactory deflaterFactory)
Creates the output stream.- Parameters:
os
- output stream to create a BlockCompressedOutputStream fromfile
- file to which to write the output or null if not availablecompressionLevel
- the compression level (0-9)deflaterFactory
- custom factory to create deflaters (overrides the default)
-
BlockCompressedOutputStream
public BlockCompressedOutputStream(OutputStream os, Path file, int compressionLevel, DeflaterFactory deflaterFactory)
Creates the output stream.- Parameters:
os
- output stream to create a BlockCompressedOutputStream fromfile
- file to which to write the output or null if not availablecompressionLevel
- the compression level (0-9)deflaterFactory
- custom factory to create deflaters (overrides the default)
-
-
Method Detail
-
setDefaultCompressionLevel
public static void setDefaultCompressionLevel(int compressionLevel)
Sets the GZip compression level for subsequent BlockCompressedOutputStream object creation that do not specify the compression level.- Parameters:
compressionLevel
- 1 <= compressionLevel <= 9
-
getDefaultCompressionLevel
public static int getDefaultCompressionLevel()
-
setDefaultDeflaterFactory
public static void setDefaultDeflaterFactory(DeflaterFactory deflaterFactory)
Sets the defaultDeflaterFactory
that will be used for all instances unless specified otherwise in the constructor. If this method is not called the default is a factory that will create the JDKDeflater
.- Parameters:
deflaterFactory
- non-null default factory.
-
getDefaultDeflaterFactory
public static DeflaterFactory getDefaultDeflaterFactory()
-
maybeBgzfWrapOutputStream
public static BlockCompressedOutputStream maybeBgzfWrapOutputStream(File location, OutputStream output)
- Parameters:
location
- May be null. Used for error messages, and for checking file termination.output
- May or not already be a BlockCompressedOutputStream.- Returns:
- A BlockCompressedOutputStream, either by wrapping the given OutputStream, or by casting if it already is a BCOS.
-
addIndexer
public void addIndexer(OutputStream outputStream)
Adds a GZIIndexer to the block compressed output stream to be written to the specified output stream. SeeGZIIndex
for details on the index. Note that the stream will be written to disk entirely when close() is called.- Throws:
RuntimeException
- this method is called after output has already been written to the stream.
-
write
public void write(byte[] bytes) throws IOException
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).- Overrides:
write
in classOutputStream
- Parameters:
bytes
- the data- Throws:
IOException
-
write
public void write(byte[] bytes, int startIndex, int numBytes) throws IOException
Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.- Overrides:
write
in classOutputStream
- Parameters:
bytes
- the datastartIndex
- the start offset in the datanumBytes
- the number of bytes to write- Throws:
IOException
-
write
public void write(int b) throws IOException
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
flush
public void flush() throws IOException
WARNING: flush() affects the output format, because it causes the current contents of uncompressedBuffer to be compressed and written, even if it isn't full. Unless you know what you're doing, don't call flush(). Instead, call close(), which will flush any unwritten data before closing the underlying stream.- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
close
public void close() throws IOException
close() must be called in order to flush any remaining buffered bytes. An unclosed file will likely be defective.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
close
public void close(boolean writeTerminatorBlock) throws IOException
- Throws:
IOException
-
getFilePointer
public long getFilePointer()
Encode virtual file pointer Upper 48 bits is the byte offset into the compressed stream of a block. Lower 16 bits is the byte offset into the uncompressed stream inside the block.
-
getPosition
public long getPosition()
Description copied from interface:LocationAware
The current offset, in bytes, of this stream/writer/file. Or, if this is an iterator/producer, the offset (in bytes) of the END of the most recently returned record (since a produced record corresponds to something that has been read already). See class javadoc for more. Note that for BGZF files, this does not represent an actually file position, but a virtual file pointer.- Specified by:
getPosition
in interfaceLocationAware
-
-