class ADSP::Stream::Writer

ADSP::Stream::Writer class.

Constants

RawCompressor

Current raw stream class.

Public Class Methods

new(destination_io, options = {}, *args) click to toggle source

Initializes stream using destination_io native stream and options. Option: :external_encoding encoding name for destination data. Option: :internal_encoding encoding name for source data. Option: :transcode_options transcode options for data.

Calls superclass method ADSP::Stream::Abstract::new
# File lib/adsp/stream/writer.rb, line 21
def initialize(destination_io, options = {}, *args)
  @options = options

  super destination_io, *args
end

Public Instance Methods

close() click to toggle source

Closes stream.

Calls superclass method ADSP::Stream::Abstract#close
# File lib/adsp/stream/writer.rb, line 73
def close
  validate_write

  finish :close

  super
end
close_nonblock(*options) click to toggle source

Closes stream nonblock. options will be passed to native stream.

# File lib/adsp/stream/writer.rb, line 154
def close_nonblock(*options)
  validate_write_nonblock

  return false unless finish_nonblock :close, *options

  method(:close).super_method.call

  true
end
flush() click to toggle source

Flushes stream.

# File lib/adsp/stream/writer.rb, line 53
def flush
  validate_write

  finish :flush

  @io.flush if @io.respond_to? :flush

  self
end
flush_nonblock(*options) click to toggle source

Flushes stream nonblock. options will be passed to native stream.

# File lib/adsp/stream/writer.rb, line 130
def flush_nonblock(*options)
  validate_write_nonblock

  return false unless finish_nonblock :flush, *options

  @io.flush if @io.respond_to? :flush

  true
end
rewind() click to toggle source

Resets stream.

Calls superclass method ADSP::Stream::Abstract#rewind
# File lib/adsp/stream/writer.rb, line 64
def rewind
  validate_write

  finish :close

  super
end
rewind_nonblock(*options) click to toggle source

Resets stream nonblock. options will be passed to native stream.

# File lib/adsp/stream/writer.rb, line 142
def rewind_nonblock(*options)
  validate_write_nonblock

  return false unless finish_nonblock :close, *options

  method(:rewind).super_method.call

  true
end
write(*objects) click to toggle source

Writes objects to stream.

# File lib/adsp/stream/writer.rb, line 35
def write(*objects)
  validate_write

  write_remaining_buffer

  bytes_written = 0

  objects.each do |object|
    source         = transcode object.to_s
    bytes_written += raw_wrapper :write, source
  end

  @pos += bytes_written

  bytes_written
end
write_nonblock(object, *options) click to toggle source

Writes object nonblock. options will be passed to native stream. Native stream write_nonblock can raise IO::WaitWritable error. After resolving this error user may provide same content again. It is not possible to revert accepted content after error. So we have to accept content after processing native stream write_nonblock. It means that first write nonblock won’t call native stream write_nonblock.

# File lib/adsp/stream/writer.rb, line 116
def write_nonblock(object, *options)
  validate_write_nonblock

  return 0 unless write_remaining_buffer_nonblock(*options)

  source         = transcode object.to_s
  bytes_written  = raw_nonblock_wrapper :write, source
  @pos          += bytes_written

  bytes_written
end

Protected Instance Methods

create_raw_stream() click to toggle source

Creates raw stream.

# File lib/adsp/stream/writer.rb, line 28
          def create_raw_stream
  self.class::RawCompressor.new @options
end
finish(method_name) click to toggle source

Finishes stream using method_name.

# File lib/adsp/stream/writer.rb, line 82
          def finish(method_name)
  write_remaining_buffer

  raw_wrapper method_name
end
finish_nonblock(method_name, *options) click to toggle source

Finishes stream using method_name nonblock. options will be passed to native stream.

# File lib/adsp/stream/writer.rb, line 166
          def finish_nonblock(method_name, *options)
  return false unless write_remaining_buffer_nonblock(*options)

  raw_nonblock_wrapper method_name

  write_remaining_buffer_nonblock(*options)
end
raw_nonblock_wrapper(method_name, *args) click to toggle source

Wraps nonblock method_name for raw stream.

# File lib/adsp/stream/writer.rb, line 188
          def raw_nonblock_wrapper(method_name, *args)
  @raw_stream.send(method_name, *args) { |portion| @buffer << portion }
end
raw_wrapper(method_name, *args) click to toggle source

Wraps method_name for raw stream.

# File lib/adsp/stream/writer.rb, line 98
          def raw_wrapper(method_name, *args)
  @raw_stream.send(method_name, *args) { |portion| @io.write portion }
end
transcode(data) click to toggle source

Transcodes data to external encoding.

# File lib/adsp/stream/writer.rb, line 200
          def transcode(data)
  data = data.encode @external_encoding, **@transcode_options unless @external_encoding.nil?
  data
end
validate_write() click to toggle source

Validates native stream responsibility to write method.

# File lib/adsp/stream/writer.rb, line 103
          def validate_write
  raise ValidateError, "io should be responsible to write" unless @io.respond_to? :write
end
validate_write_nonblock() click to toggle source

Validates native stream responsibility to write_nonblock method.

# File lib/adsp/stream/writer.rb, line 193
          def validate_write_nonblock
  raise ValidateError, "io should be responsible to write nonblock" unless @io.respond_to? :write_nonblock
end
write_remaining_buffer() click to toggle source

Writes remaining buffer and resets it.

# File lib/adsp/stream/writer.rb, line 89
          def write_remaining_buffer
  return nil if @buffer.bytesize.zero?

  @io.write @buffer

  reset_buffer
end
write_remaining_buffer_nonblock(*options) click to toggle source

Writes remaining buffer nonblock. options will be passed to native stream.

# File lib/adsp/stream/writer.rb, line 176
          def write_remaining_buffer_nonblock(*options)
  return true if @buffer.bytesize.zero?

  bytes_written = @io.write_nonblock @buffer, *options
  return false if bytes_written.zero?

  @buffer = @buffer.byteslice bytes_written, @buffer.bytesize - bytes_written

  @buffer.bytesize.zero?
end