# Class TrialCompressor

java.lang.Object
htsjdk.samtools.cram.compression.ExternalCompressor
htsjdk.samtools.cram.compression.TrialCompressor

public class TrialCompressor extends ExternalCompressor
An `ExternalCompressor` that tries multiple candidate compressors and selects the one that produces the smallest output. Matches htslib's trial compression approach.

Not thread-safe. Instances must not be shared across threads.

1. Trial phase (`nTrials` non-empty blocks): compresses with ALL candidates, picks the smallest for each block, and accumulates sizes to determine the overall winner.
2. Production phase (next `trialSpan` blocks): uses the cached winner only.
3. Re-trial: after `trialSpan` blocks, re-enters the trial phase to adapt to changing data characteristics.

The compression method is initially null and is set after the first non-empty block is compressed. Calling `ExternalCompressor.getMethod()` before any non-empty `compress(byte[], CRAMCodecModelContext)` call will throw `IllegalStateException`.

See Also:
  - `ExternalCompressor`

## Field Summary

### Fields inherited from class ExternalCompressor

`NO_COMPRESSION_ARG`

## Constructor Summary

Constructors
Constructor
Description
`TrialCompressor(List<ExternalCompressor> candidates)`
Create a trial compressor with the given candidate compressors.

## Method Summary

Modifier and Type
Method
Description
`byte[]`
`compress(byte[] data, CRAMCodecModelContext contextModel)`
Compress data.
`byte[]`
`uncompress(byte[] data)`
Decompress data.

### Methods inherited from class ExternalCompressor

`equals, getCompressorForMethod, getMethod, hashCode, setMethod, toString`

### Methods inherited from class Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

## Constructor Details

### TrialCompressor

public TrialCompressor(List<ExternalCompressor> candidates)
Create a trial compressor with the given candidate compressors.

Parameters:
  `candidates` - the candidate compressors to try (must have at least 2)

## Method Details

### compress

public byte[] compress(byte[] data, CRAMCodecModelContext contextModel)
Compress data. During the trial phase, tries all candidates and accumulates size statistics. After `nTrials` non-empty blocks, selects the overall winner and uses it exclusively until the next re-trial.

Specified by:
  `compress` in class `ExternalCompressor`
Parameters:
  `data` - the data to compress
  `contextModel` - optional codec context model
Returns:
  the compressed data from the best compressor for this block

### uncompress

public byte[] uncompress(byte[] data)
Decompress data. Delegates to the winner if one has been selected, otherwise to the first candidate. In practice, decompression is handled by the method-specific decompressor selected based on the block's compression method ID, not through this trial compressor.

Specified by:
  `uncompress` in class `ExternalCompressor`
Parameters:
  `data` - the compressed data
Returns:
  the decompressed data