Class: ADSP::Stream::Writer

Inherits:
Abstract show all
Includes:
WriterHelpers
Defined in:
lib/adsp/stream/writer.rb

Overview

ADSP::Stream::Writer class.

Direct Known Subclasses

Test::Mock::Stream::Writer

Constant Summary collapse

RawCompressor =

Current raw stream class.

Raw::Compressor

Constants included from Delegates

Delegates::DELEGATES

Instance Attribute Summary

Attributes inherited from Abstract

#external_encoding, #internal_encoding, #io, #pos, #stat, #transcode_options

Instance Method Summary collapse

Methods included from WriterHelpers

#<<, included, #print, #printf, #putc, #puts

Methods inherited from Abstract

#advise, #closed?, #set_encoding, #to_io

Methods included from Delegates

included

Constructor Details

#initialize(destination_io, options = {}, *args) ⇒ Writer

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.



21
22
23
24
25
# File 'lib/adsp/stream/writer.rb', line 21

def initialize(destination_io, options = {}, *args)
  @options = options

  super destination_io, *args
end

Instance Method Details

#closeObject

Closes stream.



73
74
75
76
77
78
79
# File 'lib/adsp/stream/writer.rb', line 73

def close
  validate_write

  finish :close

  super
end

#close_nonblock(*options) ⇒ Object

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



154
155
156
157
158
159
160
161
162
# 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

#flushObject

Flushes stream.



53
54
55
56
57
58
59
60
61
# 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) ⇒ Object

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



130
131
132
133
134
135
136
137
138
# 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

#rewindObject

Resets stream.



64
65
66
67
68
69
70
# File 'lib/adsp/stream/writer.rb', line 64

def rewind
  validate_write

  finish :close

  super
end

#rewind_nonblock(*options) ⇒ Object

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



142
143
144
145
146
147
148
149
150
# 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) ⇒ Object

Writes objects to stream.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Object

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.



116
117
118
119
120
121
122
123
124
125
126
# 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