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



17
18
19
20
21
22
# File 'lib/brs/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



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

def close(&writer)
  return nil if closed?

  Validation.validate_proc writer

  loop do
    need_more_destination = @native_stream.finish

    if need_more_destination
      flush_destination_buffer(&writer)
      next
    end

    break
  end

  super

  nil
end

#flush(&writer) ⇒ Object



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

def flush(&writer)
  do_not_use_after_close

  Validation.validate_proc writer

  loop do
    need_more_destination = @native_stream.flush

    if need_more_destination
      flush_destination_buffer(&writer)
      next
    end

    break
  end

  super

  nil
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
# File 'lib/brs/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
      flush_destination_buffer(&writer)
      next
    end

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

    break
  end

  total_bytes_written
end