Class: Omnizip::Algorithms::LZMA2XzEncoderAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/algorithms/lzma2/xz_encoder_adapter.rb

Overview

Adapter for XZ Encoder to work with LZMA2 chunking

Wraps the pure Ruby XZ encoder to provide LZMA2-compatible interface for chunked encoding with size limits.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LZMA2XzEncoderAdapter

Initialize XZ encoder adapter

Parameters:

  • options (Hash) (defaults to: {})

    Encoding options

Options Hash (options):

  • :lc (Integer)

    Literal context bits (default 3)

  • :lp (Integer)

    Literal position bits (default 0)

  • :pb (Integer)

    Position bits (default 2)

  • :nice_len (Integer)

    Nice match length (default 32)

  • :dict_size (Integer)

    Dictionary size (default 8MB)



38
39
40
41
42
43
# File 'lib/omnizip/algorithms/lzma2/xz_encoder_adapter.rb', line 38

def initialize(options = {})
  @options = options
  @lc = options[:lc] || 3
  @lp = options[:lp] || 0
  @pb = options[:pb] || 2
end

Instance Method Details

#dict_sizeInteger

Get dictionary size

Returns:

  • (Integer)

    Dictionary size in bytes



75
76
77
# File 'lib/omnizip/algorithms/lzma2/xz_encoder_adapter.rb', line 75

def dict_size
  @options[:dict_size] || (1 << 23) # 8MB default
end

#encode_chunk(data, _limit = nil) ⇒ String

Encode data chunk

Parameters:

  • data (String)

    Input data to encode

  • limit (Integer, nil)

    Optional output size limit

Returns:

  • (String)

    Encoded data



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/omnizip/algorithms/lzma2/xz_encoder_adapter.rb', line 50

def encode_chunk(data, _limit = nil)
  output = StringIO.new
  encoder = LZMA::XzEncoder.new(@options)

  # Encode with optional size limit
  # XZ encoder returns bytes written to output
  encoder.encode(data, output)

  # Return the encoded data string
  output.string
end

#propertiesInteger

Get LZMA properties byte

Encodes lc, lp, pb into single byte using formula: (pb * 5 + lp) * 9 + lc

Returns:

  • (Integer)

    Properties byte (0x00-0xFF)



68
69
70
# File 'lib/omnizip/algorithms/lzma2/xz_encoder_adapter.rb', line 68

def properties
  (((@pb * 5) + @lp) * 9) + @lc
end