RLEBCompressor

class tinycompress.rleb.RLEBCompressor(minsame=3, minpast=0)[source]

RLEB compression implementation.

This compressor implements Run-Length Encoding for Bytes (RLEB), which efficiently compresses repeated byte sequences while maintaining good performance for non-repeated data.

The algorithm uses a ring buffer to track recent bytes and detect repetitions. When MINSAME_MIN or more identical bytes are found, they are encoded as a run. Otherwise, bytes are stored literally with a count.

Attributes

eof

Whether the compressor has been flushed.

Methods

__init__

Initializes a new RLEB compressor instance.

compress

Compresses the given data using RLEB encoding.

flush

Flushes any remaining data from the compressor.

reset

Resets the compressor to its initial state.

__init__(minsame=3, minpast=0)[source]

Initializes a new RLEB compressor instance.

The compressor starts with an empty ring buffer and no previous byte tracked. The end-of-file flag is initially False.

Parameters:
  • minsame (int) – Minimum compressed size, within MINSAME_MIN and MINSAME_MAX.

  • minpast (int) – Minimum bytes after a same streak, within MINPAST_MIN and MINPAST_MAX.

Raises:

ValueError – If any parameters are out of their valid ranges.

__weakref__

list of weak references to the object

compress(data)[source]

Compresses the given data using RLEB encoding.

The compression maintains a ring buffer and tracks repeated bytes. When MINSAME_MIN or more identical bytes are found, they are encoded as a run using a flag byte and count. Non-repeated sequences are stored with their literal values.

Parameters:

data (Union[bytes, bytearray, memoryview, Iterable[int]]) – Input data to compress.

Returns:

Compressed data as a bytearray.

Raises:

RLEBException – If the compressor has already been flushed.

property eof: bool

Whether the compressor has been flushed.

Returns:

True if flush() has been called, False otherwise.

flush()[source]

Flushes any remaining data from the compressor.

This outputs any pending data in the ring buffer and marks the compressor as finished. After calling flush(), the compressor cannot be used again unless reset.

Returns:

Any remaining compressed data as a bytearray.

reset()[source]

Resets the compressor to its initial state.

This clears all internal buffers and state, allowing the compressor to be reused for a new compression task.