class ADSP::Stream::Writer
ADSP::Stream::Writer
class.
Constants
- RawCompressor
Current raw stream class.
Public Class Methods
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.
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
Closes stream.
ADSP::Stream::Abstract#close
# File lib/adsp/stream/writer.rb, line 73 def close validate_write finish :close super end
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
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
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
Resets stream.
ADSP::Stream::Abstract#rewind
# File lib/adsp/stream/writer.rb, line 64 def rewind validate_write finish :close super end
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
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
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
Creates raw stream.
# File lib/adsp/stream/writer.rb, line 28 def create_raw_stream self.class::RawCompressor.new @options end
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
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
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
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
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
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
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
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
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