class

HPack::Huffman

Inherits Reference < Object

Constructors

new(table)
Source

Instance methods

decode(bytes : Bytes, dest : Bytes) : Int32

Huffman-decodes bytes into dest, starting at offset 0, writing through a raw pointer rather than a virtual IO#write_byte sink.

Returns the number of bytes written. Returns -1 if dest is too small to hold the fully decoded output; callers that enforce a decoded size cap (Decoder#max_decoded_string_size) treat that return value as a size-violation rather than retrying with a larger buffer. Raises HPack::Error on invalid Huffman padding or an encoded EOS symbol, same as the String-returning overload below.

Source
decode(bytes : Bytes, output : IO) : Nil

Decodes bytes and writes the result to output.

This method does not clear or rewind output. The caller owns and controls the output buffer.

A thin compatibility wrapper over the pointer-sink #decode(bytes, dest) overload: it allocates a local destination sized to the maximum possible decoded output (same bound as the String-returning overload above), so the -1 (undersized-destination) case can never occur here and is not checked for.

Source
decode(bytes : Bytes) : String

Huffman-decodes bytes and returns a freshly allocated String.

Allocates a local destination buffer sized to the maximum possible decoded output (Huffman codes are at least 5 bits, so decoded output never exceeds bytes.size * 8 // 5 + 1 bytes); no shared or fiber-cached scratch buffer is used.

Source
encode(string : String, dest : Bytes, bit_length : Int64) : Int32

Huffman-encodes string into dest, starting at offset 0.

bit_length must equal encoded_bit_length(string) for this same string — it is trusted as a sizing hint, not re-derived, so the single sizing walk stays in the caller. That equality is verified as writes happen, not merely assumed: every store into dest is bounds checked against dest.size (raising HPack::Error rather than corrupting memory through the unsafe pointer this method writes through if bit_length understates the true length), and once the string has been fully walked, the number of bytes actually written is compared against the (bit_length + 7) // 8 byte count the caller implied; a mismatch (understated or overstated bit_length) raises HPack::Error instead of silently returning a byte count that includes stale, previously-written dest contents.

On success, writes exactly (bit_length + 7) // 8 bytes and returns that count. dest must be at least that many bytes long; the caller (the Encoder's scratch buffer, in the common case) owns sizing it.

Source
encode(string : String) : Bytes

Huffman-encodes string and returns a freshly allocated Bytes.

Source
encoded_bit_length(string : String) : Int64

Returns the number of bits needed to Huffman-encode string.

Source
encoded_size(string : String) : Int32

Returns the byte size needed to Huffman-encode string.

Raises HPack::Error if the encoded result cannot fit in a Crystal Bytes value.

Source
encoded_size(bit_length : Int64) : Int32

Returns the byte size needed to Huffman-encode a string whose encoded bit length is bit_length (as returned by #encoded_bit_length).

Raises HPack::Error if the encoded result cannot fit in a Crystal Bytes value. Public so callers that already have a bit length in hand (notably Encoder#string) can compute the byte size without duplicating this arithmetic.

Source