Class: ADSP::Stream::Raw::Compressor

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

Overview

ADSP::Stream::Raw::Compressor class.

Direct Known Subclasses

Test::Mock::Stream::Raw::Compressor

Constant Summary collapse

NativeCompressor =

Current native compressor class.

Raw::NativeCompressor
Option =

Current option class.

ADSP::Option
BUFFER_LENGTH_NAMES =

Current buffer length names. It is a part of compressor options.

i[destination_buffer_length].freeze

Instance Method Summary collapse

Methods inherited from Abstract

#closed?

Constructor Details

#initialize(options = {}) ⇒ Compressor

Initializes compressor. Option: :destination_buffer_length destination buffer length.



27
28
29
30
31
32
# File 'lib/adsp/stream/raw/compressor.rb', line 27

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

  super native_stream
end

Instance Method Details

#close(&writer) ⇒ Object

Finishes compressor, writes result using writer proc and closes compressor. Raises UsedAfterCloseError when used after close.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/adsp/stream/raw/compressor.rb', line 89

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

Flushes compressor, writes result using writer proc and closes compressor.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/adsp/stream/raw/compressor.rb', line 68

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

Writes source string, writes result using writer proc. Returns amount of bytes written from source.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/adsp/stream/raw/compressor.rb', line 36

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
      # Compressor write should eat all provided "source" without remainder.
      # :nocov:
      raise UnexpectedError, "unexpected error"
      # :nocov:
    end

    break
  end

  total_bytes_written
end