Class: Omnizip::Algorithms::Zstandard::LiteralsEncoder

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/algorithms/zstandard/literals_encoder.rb

Overview

Literals Section Encoder (RFC 8878 Section 3.1.1.3.1)

Encodes literals sections for Zstandard compressed blocks. Supports raw, RLE, and Huffman-compressed literals.

Constant Summary

Constants included from Constants

Constants::BLOCK_HEADER_SIZE, Constants::BLOCK_MAX_SIZE, Constants::BLOCK_TYPE_COMPRESSED, Constants::BLOCK_TYPE_RAW, Constants::BLOCK_TYPE_RESERVED, Constants::BLOCK_TYPE_RLE, Constants::BUFFER_SIZE, Constants::DEFAULT_LEVEL, Constants::DEFAULT_REPEAT_OFFSETS, Constants::FSE_MAX_ACCURACY_LOG, Constants::FSE_MIN_ACCURACY_LOG, Constants::HUFFMAN_MAX_BITS, Constants::HUFFMAN_MAX_CODE_LENGTH, Constants::HUFFMAN_MAX_LOG, Constants::HUFFMAN_STANDARD_TABLE_SIZE, Constants::LITERALS_BLOCK_COMPRESSED, Constants::LITERALS_BLOCK_RAW, Constants::LITERALS_BLOCK_RLE, Constants::LITERALS_BLOCK_TREELESS, Constants::LITERALS_LENGTH_ACCURACY_LOG, Constants::LITERAL_LENGTH_TABLE, Constants::MAGIC_BYTES, Constants::MAGIC_NUMBER, Constants::MATCH_LENGTH_ACCURACY_LOG, Constants::MATCH_LENGTH_TABLE, Constants::MAX_LEVEL, Constants::MIN_LEVEL, Constants::MODE_FSE, Constants::MODE_PREDEFINED, Constants::MODE_REPEAT, Constants::MODE_RLE, Constants::OFFSET_ACCURACY_LOG, Constants::PREDEFINED_LL_DISTRIBUTION, Constants::PREDEFINED_ML_DISTRIBUTION, Constants::PREDEFINED_OFFSET_DISTRIBUTION, Constants::REPEAT_OFFSET_1, Constants::REPEAT_OFFSET_2, Constants::REPEAT_OFFSET_3, Constants::SKIPPABLE_MAGIC_BASE, Constants::SKIPPABLE_MAGIC_MASK, Constants::WINDOW_LOG_MAX, Constants::WINDOW_LOG_MIN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(literals, previous_huffman = nil, use_compression = true) ⇒ LiteralsEncoder

Initialize literals encoder

Parameters:

  • literals (String)

    Literal bytes

  • previous_huffman (HuffmanEncoder, nil) (defaults to: nil)

    Previous Huffman encoder

  • use_compression (Boolean) (defaults to: true)

    Whether to use compression



52
53
54
55
56
57
# File 'lib/omnizip/algorithms/zstandard/literals_encoder.rb', line 52

def initialize(literals, previous_huffman = nil, use_compression = true)
  @literals = literals.to_s.dup.force_encoding(Encoding::BINARY)
  @previous_huffman = previous_huffman
  @use_compression = use_compression
  @huffman_encoder = nil
end

Instance Attribute Details

#huffman_encoderHuffmanEncoder? (readonly)

Returns Huffman encoder for this block.

Returns:



34
35
36
# File 'lib/omnizip/algorithms/zstandard/literals_encoder.rb', line 34

def huffman_encoder
  @huffman_encoder
end

Class Method Details

.encode(literals, previous_huffman: nil, use_compression: true) ⇒ String

Encode literals section

Parameters:

  • literals (String)

    Literal bytes to encode

  • previous_huffman (HuffmanEncoder, nil) (defaults to: nil)

    Previous Huffman encoder (for treeless)

  • use_compression (Boolean) (defaults to: true)

    Whether to use Huffman compression

Returns:

  • (String)

    Encoded literals section



42
43
44
45
# File 'lib/omnizip/algorithms/zstandard/literals_encoder.rb', line 42

def self.encode(literals, previous_huffman: nil, use_compression: true)
  encoder = new(literals, previous_huffman, use_compression)
  encoder.encode_section
end

Instance Method Details

#encode_sectionString

Encode the literals section

Returns:

  • (String)

    Encoded section



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/omnizip/algorithms/zstandard/literals_encoder.rb', line 62

def encode_section
  return encode_empty if @literals.empty?

  # Choose encoding method based on data characteristics
  if rle_efficient?
    encode_rle
  elsif @use_compression && huffman_efficient?
    encode_huffman
  else
    encode_raw
  end
end