Class: Omnizip::Algorithms::BZip2::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/algorithms/bzip2/encoder.rb

Overview

BZip2 Encoder

Orchestrates the full BZip2 compression pipeline:

  1. Block splitting (configurable block size)
  2. Burrows-Wheeler Transform (BWT)
  3. Move-to-Front Transform (MTF)
  4. Run-Length Encoding (RLE)
  5. Huffman Coding
  6. CRC32 checksum calculation

Each block is compressed independently for better parallelization potential and error recovery.

Constant Summary collapse

MIN_BLOCK_SIZE =

Block size constants (in bytes)

100_000
MAX_BLOCK_SIZE =

100KB

900_000
DEFAULT_BLOCK_SIZE =

900KB

900_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, options = {}) ⇒ Encoder

Initialize encoder

Parameters:

  • output (IO)

    Output stream

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

    Encoding options

Options Hash (options):

  • :block_size (Integer)

    Block size in bytes



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

def initialize(output, options = {})
  @output = output
  @block_size = validate_block_size(
    options[:block_size] || DEFAULT_BLOCK_SIZE,
  )
  @bwt = Bwt.new
  @mtf = Mtf.new
  @rle = Rle.new
  @huffman = Huffman.new
end

Instance Attribute Details

#block_sizeObject (readonly)

Returns the value of attribute block_size.



39
40
41
# File 'lib/omnizip/algorithms/bzip2/encoder.rb', line 39

def block_size
  @block_size
end

#outputObject (readonly)

Returns the value of attribute output.



39
40
41
# File 'lib/omnizip/algorithms/bzip2/encoder.rb', line 39

def output
  @output
end

Instance Method Details

#encode_stream(input) ⇒ void

This method returns an undefined value.

Encode stream using BZip2 algorithm

Parameters:

  • input (String)

    Input data to encode



66
67
68
69
70
71
72
# File 'lib/omnizip/algorithms/bzip2/encoder.rb', line 66

def encode_stream(input)
  return if input.empty?

  # Split into blocks and encode each
  blocks = split_into_blocks(input)
  blocks.each { |block| encode_block(block) }
end