Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

compress.brotli

import compress

compress exposes this as compress.brotli, so import compress is enough and the names are called as compress.brotli.*. import compress.brotli reaches the same definitions directly.

The Brotli submodule for the compress module.

Functions

compress()

compress.brotli.compress(data, quality, window) -> bytes

Compress data using Brotli.

Parameters

  • data (bytes|string)
  • quality (?number) — Compression quality (0..=11, higher is slower but smaller): Default 11.
  • window (?number) — Base-2 logarithm of the sliding window size (10..=24): Default 22.

Returns bytes

decompress()

compress.brotli.decompress(data) -> bytes

Decompress Brotli-compressed data.

Parameters

  • data (bytes|string)

Returns bytes

Classes

BrotliEncoder

class compress.brotli.BrotliEncoder

A streaming Brotli encoder.

Example:

%> import compress.brotli
%> var encoder = brotli.BrotliEncoder()
%> encoder.write('hello world')
11
%> encoder.finish()
(1b 0a 00 f8 25 04 92 68 65 6c ...)

Note: Unlike the other streaming encoders in this module (Gzip, Bzip2, Zstd, Lz4), this one does not compress incrementally as write() is called: it buffers everything you give it and does the real work once, in finish(). available() therefore always reads 0 before finish() is called: there’s nothing waiting yet. This follows from the underlying encoder only exposing a “compress this all at once into a sink” interface, not a lower-level incremental one.

Constructor

compress.brotli.BrotliEncoder(quality, window)

Creates a new streaming Brotli encoder.

Parameters

  • quality (?number) — Compression quality (0..=11): Default 11.
  • window (?number) — Base-2 logarithm of the sliding window size (10..=24): Default 22.

BrotliEncoder.finish()

compress.brotli.BrotliEncoder.finish() -> bytes

Compresses everything written so far and returns the resulting byte stream.

Returns bytes

Note: Once finish is called, the encoder can no longer be reused.

BrotliEncoder.close()

compress.brotli.BrotliEncoder.close() -> bytes

Same as finish() for file API compartibility.

Returns bytes

Note: Once finish is called, the encoder can no longer be reused.

BrotliEncoder.reset()

compress.brotli.BrotliEncoder.reset() -> bytes

Finishes the current stream and installs a fresh one for the next frame.

Returns the previous byte stream containing the completed frame.

Returns bytes

BrotliEncoder.write()

compress.brotli.BrotliEncoder.write(data) -> number

Buffers data to be compressed once finish() or reset() is called, returning how many bytes were accepted.

Parameters

  • data (bytes|string)

Returns number

BrotliEncoder.available()

compress.brotli.BrotliEncoder.available() -> number

Returns the number of compressed bytes currently waiting in the encoder’s output buffer. Always 0 before finish(): see the class-level note on why this encoder isn’t incremental.

Returns number

BrotliEncoder.finished()

compress.brotli.BrotliEncoder.finished() -> bool

Returns whether finish() has completed the stream.

Returns bool

BrotliEncoder.total_in()

compress.brotli.BrotliEncoder.total_in() -> number

Number of uncompressed bytes consumed by the encoder so far (only updated once finish()/reset() actually compresses them: see the class-level note).

Returns number

BrotliEncoder.total_out()

compress.brotli.BrotliEncoder.total_out() -> number

Number of compressed bytes generated by the encoder so far.

Returns number

BrotliDecoder

class compress.brotli.BrotliDecoder

Streaming Brotli decompressor implementation.

Wraps a byte stream of compressed data and yields decompressed bytes.

Example:

%> import compress.brotli
%> var data = brotli.compress('hello world')
%> var decoder = brotli.BrotliDecoder(data)
%> decoder.read_as_string()
hello world

Constructor

compress.brotli.BrotliDecoder(source)

Creates a Brotli decoder

Parameters

  • source (bytes)

BrotliDecoder.reset()

compress.brotli.BrotliDecoder.reset(new_source)

Installs a new data source, discarding whatever remained of the previous one.

Parameters

  • new_source (bytes)

BrotliDecoder.close()

compress.brotli.BrotliDecoder.close()

Closes the decoder by resetting it into an empty stream.

BrotliDecoder.read()

compress.brotli.BrotliDecoder.read(length) -> bytes

Reads some bytes up to the amount of bytes specified by length from the current source. Returns an empty byte stream when there is no more data to read.

Parameters

  • length (number)

Returns bytes

BrotliDecoder.read_exact()

compress.brotli.BrotliDecoder.read_exact(length) -> bytes

Reads the exact number of bytes from the buffer. This method will raise an error if it encounters an unexpected EOF (end of file) or there are insufficient data to read to complete the required number of bytes.

Parameters

  • length (number)

Returns bytes

BrotliDecoder.read_all()

compress.brotli.BrotliDecoder.read_all() -> bytes

Reads all remaining bytes until EOF is encountered in the source.

Returns bytes

BrotliDecoder.read_as_string()

compress.brotli.BrotliDecoder.read_as_string() -> string

Reads all remaining bytes until EOF is encountered in the source and returns the data read as a string instead of a byte stream.

Returns string

BrotliDecoder.available()

compress.brotli.BrotliDecoder.available() -> number

Returns the number of compressed bytes currently waiting in the decoder’s output buffer. Always 0: this decoder decodes on demand rather than buffering ahead.

Returns number

BrotliDecoder.finished()

compress.brotli.BrotliDecoder.finished() -> bool

Returns whether the decoder has reached the end of the stream.

Returns bool

BrotliDecoder.total_in()

compress.brotli.BrotliDecoder.total_in() -> number

Number of compressed bytes in the decoder’s source.

Returns number

BrotliDecoder.total_out()

compress.brotli.BrotliDecoder.total_out() -> number

Number of uncompressed bytes generated by the decoder so far.

Returns number


2026, Richard Ore and Zuri contributors