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
Whether the compressor has been flushed.
Methods
Initializes a new RLEB compressor instance.
Compresses the given data using RLEB encoding.
Flushes any remaining data from the compressor.
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.