Class: BRS::Stream::Raw::Compressor

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

Constant Summary collapse

BUFFER_LENGTH_NAMES =
i[destination_buffer_length].freeze

Instance Method Summary collapse

Methods inherited from Abstract

#closed?

Constructor Details

#initialize(options = {}) ⇒ Compressor

Returns a new instance of Compressor.



17
18
19
20
21
22
23
24
25
26
# File 'lib/brs/stream/raw/compressor.rb', line 17

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

  size_hint = options[:size_hint]
  Validation.validate_not_negative_integer size_hint unless size_hint.nil?

  native_stream = NativeCompressor.new options

  super native_stream
end

Instance Method Details

#close(&writer) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/brs/stream/raw/compressor.rb', line 78

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

#flush(&writer) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/brs/stream/raw/compressor.rb', line 59

def flush(&writer)
  do_not_use_after_close

  Validation.validate_proc writer

  loop do
    need_more_destination = @native_stream.flush

    if need_more_destination
      more_destination(&writer)
      next
    end

    break
  end

  super
end

#write(source, &writer) ⇒ Object



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
54
55
56
57
# File 'lib/brs/stream/raw/compressor.rb', line 28

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