Class: LZWS::Stream::Raw::Compressor

Inherits:
Abstract
  • Object
show all
Defined in:
lib/lzws/stream/raw/compressor.rb

Constant Summary collapse

BUFFER_LENGTH_NAMES =
i[destination_buffer_length].freeze

Instance Method Summary collapse

Methods inherited from Abstract

#closed?, #flush

Constructor Details

#initialize(options = {}) ⇒ Compressor

Returns a new instance of Compressor.



17
18
19
20
21
22
# File 'lib/lzws/stream/raw/compressor.rb', line 17

def initialize(options = {})
  options       = Option.get_compressor_options options, BUFFER_LENGTH_NAMES
  native_stream = NativeCompressor.new options

  super native_stream
end

Instance Method Details

#close(&writer) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lzws/stream/raw/compressor.rb', line 55

def close(&writer)
  return nil if closed?

  Validation.validate_proc writer

  loop do
    need_more_destination = @native_stream.finish

    if need_more_destination
      more_destination(&writer)
      next
    end

    break
  end

  super
end

#write(source, &writer) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lzws/stream/raw/compressor.rb', line 24

def write(source, &writer)
  do_not_use_after_close

  Validation.validate_string source
  Validation.validate_proc writer

  total_bytes_written = 0

  loop do
    bytes_written, need_more_destination  = @native_stream.write source
    total_bytes_written                  += bytes_written

    if need_more_destination
      source = source.byteslice bytes_written, source.bytesize - bytes_written
      more_destination(&writer)
      next
    end

    unless bytes_written == source.bytesize
      # :nocov:
      # Compressor write should eat all provided "source" without remainder.
      raise UnexpectedError, "unexpected error"
      # :nocov:
    end

    break
  end

  total_bytes_written
end