Bzip2 compression codec (pure-Rust backend).
Options
:block_size— 1..9 (default 9):max_output_size— Maximum allowed decompressed size in bytes (default: 256 MiB)
Security
Do not decompress untrusted inputs without a tight :max_output_size.
Examples
iex> {:ok, compressed} = ExCodecs.encode(:bzip2, "hello world")
iex> {:ok, decompressed} = ExCodecs.decode(:bzip2, compressed)
iex> decompressed
"hello world"
iex> {:ok, compressed} = ExCodecs.encode(:bzip2, "hello world", block_size: 6)
iex> is_binary(compressed)
true
Summary
Functions
Returns the registry metadata for the Bzip2 codec.
Decompresses Bzip2 data.
Compresses a binary with Bzip2.
Functions
Returns the registry metadata for the Bzip2 codec.
Arguments
This function takes no arguments.
Returns
An ExCodecs.Codec.t() with these Bzip2-specific fields:
name: :bzip2andcategory: :compressionmodule: ExCodecs.Compression.Bzip2native?: truebecause compression runs in a NIFstreaming?: falsebecause only complete payloads are supportedconfigurable?: truebecauseencode/2accepts:block_sizeversion: "bzip2-0.6/libbz2-rs"for the backend implementation
Raises / Exceptions
This function does not invoke the NIF and does not raise.
Examples
iex> ExCodecs.Compression.Bzip2.__codec_info__()
%ExCodecs.Codec{
name: :bzip2,
category: :compression,
module: ExCodecs.Compression.Bzip2,
native?: true,
streaming?: false,
configurable?: true,
version: "bzip2-0.6/libbz2-rs"
}
Decompresses Bzip2 data.
Arguments
data(binary()) — a complete Bzip2 streamopts(keyword()) — optional:max_output_size(positive integer bytes, default 256 MiB)
Returns
{:ok, decompressed :: binary()}on success{:error, %ExCodecs.Error{reason: :invalid_data}}whendatais not a binary,optsis not a list, or the NIF raises an argument error{:error, %ExCodecs.Error{reason: :invalid_options}}when:max_output_sizeis not a positive integer{:error, %ExCodecs.Error{reason: :output_limit_exceeded}}when the decompressed size would exceed:max_output_size{:error, %ExCodecs.Error{reason: :decompression_failed}}whendatais corrupt, truncated, or not a Bzip2 stream{:error, %ExCodecs.Error{reason: :nif_not_loaded}}when the native library is unavailable
Raises / Exceptions
Guard failures and ErlangError/ArgumentError exceptions from the NIF
call are converted to error tuples. Unexpected exception classes may
propagate.
Examples
iex> payload = "quarterly results"
iex> {:ok, compressed} = ExCodecs.Compression.Bzip2.encode(payload, [])
iex> ExCodecs.Compression.Bzip2.decode(compressed, [])
{:ok, "quarterly results"}
iex> {:error, error} = ExCodecs.Compression.Bzip2.decode("not bzip2", [])
iex> error.reason
:decompression_failed
Compresses a binary with Bzip2.
Arguments
data(binary()) — uncompressed bytesopts(keyword()) — options containing:block_size, an integer from1(100 KiB blocks) through9(900 KiB blocks); defaults to9. Unknown keys are ignored.
Returns
{:ok, compressed :: binary()}containing a Bzip2 stream{:error, %ExCodecs.Error{reason: :invalid_data}}whendatais not a binary,optsis not a list, or the NIF raises an argument error{:error, %ExCodecs.Error{reason: :invalid_options}}when:block_sizeis not an integer in1..9{:error, %ExCodecs.Error{reason: :compression_failed}}when the native compressor fails{:error, %ExCodecs.Error{reason: :nif_not_loaded}}when the native library is unavailable
Raises / Exceptions
Guard/option validation failures and ErlangError/ArgumentError
exceptions from the NIF call are converted to error tuples. Unexpected
exception classes may propagate.
Examples
iex> payload = :binary.copy("daily-report,", 10)
iex> {:ok, compressed} =
...> ExCodecs.Compression.Bzip2.encode(payload, block_size: 6)
iex> is_binary(compressed)
true
iex> ExCodecs.Compression.Bzip2.decode(compressed, [])
{:ok, payload}
iex> {:error, error} = ExCodecs.Compression.Bzip2.encode("data", block_size: 10)
iex> error.reason
:invalid_options