# Class GzipCodec

java.lang.Object
htsjdk.samtools.util.GzipCodec

public class GzipCodec extends Object
A reusable codec for compressing and decompressing GZIP and BGZF data using direct `Deflater`/`Inflater` operations on `ByteBuffer`s. Designed to be allocated once and reused across many compress/decompress operations.

Supports two output formats for compression:

- `GzipCodec.Format.GZIP` — standard 10-byte GZIP header (RFC 1952)
- `GzipCodec.Format.BGZF` — BGZF header with BC extra subfield (SAM/BAM spec)

Decompression handles both formats transparently by parsing the FLG byte and skipping any optional GZIP fields.

Not thread-safe. Use one instance per thread.

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static enum`
`GzipCodec.Format`
The output format for compression.

## Constructor Summary

Constructors
Constructor
Description
`GzipCodec()`
Create a codec with the default compression level and default strategy.
`GzipCodec(int compressionLevel)`
Create a codec with the specified compression level and default strategy.
`GzipCodec(int compressionLevel, int deflateStrategy)`
Create a codec with the specified compression level and deflate strategy.
`GzipCodec(int compressionLevel, int deflateStrategy, DeflaterFactory deflaterFactory, InflaterFactory inflaterFactory)`
Create a codec with full control over compression parameters and factory implementations.

## Method Summary

Modifier and Type
Method
Description
`ByteBuffer`
`compress(ByteBuffer input)`
Compress data and return a new ByteBuffer containing the compressed result.
`ByteBuffer`
`compress(ByteBuffer input, GzipCodec.Format format)`
Compress data and return a new ByteBuffer containing the compressed result.
`int`
`compress(ByteBuffer input, ByteBuffer output)`
Compress data from `input` into `output` using standard GZIP format.
`int`
`compress(ByteBuffer input, ByteBuffer output, GzipCodec.Format format)`
Compress data from `input` into `output` using the specified format.
`ByteBuffer`
`decompress(ByteBuffer input)`
Decompress GZIP or BGZF data and return a new ByteBuffer containing the result.
`int`
`decompress(ByteBuffer input, ByteBuffer output)`
Decompress GZIP or BGZF data from `input` into `output`.
`void`
`setCheckCrcs(boolean check)`
Enable or disable CRC32 validation during decompression.

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### GzipCodec

public GzipCodec()
Create a codec with the default compression level and default strategy.

### GzipCodec

public GzipCodec(int compressionLevel)
Create a codec with the specified compression level and default strategy.

### GzipCodec

public GzipCodec(int compressionLevel, int deflateStrategy)
Create a codec with the specified compression level and deflate strategy.

### GzipCodec

public GzipCodec(int compressionLevel, int deflateStrategy, DeflaterFactory deflaterFactory, InflaterFactory inflaterFactory)
Create a codec with full control over compression parameters and factory implementations.

Parameters:
  `compressionLevel` - deflate compression level (0-9)
  `deflateStrategy` - deflate strategy (e.g., `Deflater.DEFAULT_STRATEGY`, `Deflater.FILTERED`)
  `deflaterFactory` - factory for creating Deflater instances
  `inflaterFactory` - factory for creating Inflater instances

## Method Details

### setCheckCrcs

public void setCheckCrcs(boolean check)
Enable or disable CRC32 validation during decompression.

### compress

public int compress(ByteBuffer input, ByteBuffer output)
Compress data from `input` into `output` using standard GZIP format.

Parameters:
  `input` - data to compress (from position to limit; position is advanced to limit)
  `output` - buffer to write compressed data into (from position; position is advanced)
Returns:
  number of bytes written to output

### compress

public int compress(ByteBuffer input, ByteBuffer output, GzipCodec.Format format)
Compress data from `input` into `output` using the specified format.

Parameters:
  `input` - data to compress (from position to limit; position is advanced to limit)
  `output` - buffer to write compressed data into (from position; position is advanced)
  `format` - the output format (`GzipCodec.Format.GZIP` or `GzipCodec.Format.BGZF`)
Returns:
  number of bytes written to output

### compress

public ByteBuffer compress(ByteBuffer input)
Compress data and return a new ByteBuffer containing the compressed result.

Parameters:
  `input` - data to compress (from position to limit; position is advanced to limit)
Returns:
  a new ByteBuffer containing the compressed data, positioned at 0 with limit at the end

### compress

public ByteBuffer compress(ByteBuffer input, GzipCodec.Format format)
Compress data and return a new ByteBuffer containing the compressed result.

Parameters:
  `input` - data to compress (from position to limit; position is advanced to limit)
  `format` - the output format
Returns:
  a new ByteBuffer containing the compressed data, positioned at 0 with limit at the end

### decompress

public int decompress(ByteBuffer input, ByteBuffer output)
Decompress GZIP or BGZF data from `input` into `output`. Handles both standard GZIP and BGZF transparently.

Parameters:
  `input` - compressed data (from position to limit; position is advanced)
  `output` - buffer to write decompressed data into (from position; position is advanced)
Returns:
  number of decompressed bytes written to output

### decompress

public ByteBuffer decompress(ByteBuffer input)
Decompress GZIP or BGZF data and return a new ByteBuffer containing the result. Reads the ISIZE field from the GZIP trailer to determine the output size.

Parameters:
  `input` - compressed data (from position to limit; position is advanced)
Returns:
  a new ByteBuffer containing the decompressed data, positioned at 0 with limit at the end