Class: BRS::Stream::Writer

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

Constant Summary

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

Returns a new instance of Writer.



13
14
15
16
17
# File 'lib/brs/stream/writer.rb', line 13

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

  super destination_io, *args
end

Instance Method Details

#closeObject



60
61
62
63
64
65
66
# File 'lib/brs/stream/writer.rb', line 60

def close
  validate_write

  finish :close

  super
end

#close_nonblock(*options) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/brs/stream/writer.rb', line 129

def close_nonblock(*options)
  validate_write_nonblock

  return false unless finish_nonblock :close, *options

  method(:close).super_method.call

  true
end

#flushObject



42
43
44
45
46
47
48
49
50
# File 'lib/brs/stream/writer.rb', line 42

def flush
  validate_write

  finish :flush

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

  self
end

#flush_nonblock(*options) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/brs/stream/writer.rb', line 109

def flush_nonblock(*options)
  validate_write_nonblock

  return false unless finish_nonblock :flush, *options

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

  true
end

#rewindObject



52
53
54
55
56
57
58
# File 'lib/brs/stream/writer.rb', line 52

def rewind
  validate_write

  finish :close

  super
end

#rewind_nonblock(*options) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/brs/stream/writer.rb', line 119

def rewind_nonblock(*options)
  validate_write_nonblock

  return false unless finish_nonblock :close, *options

  method(:rewind).super_method.call

  true
end

#validate_writeObject

Raises:



86
87
88
# File 'lib/brs/stream/writer.rb', line 86

def validate_write
  raise ValidateError, "io should be responsible to write" unless @io.respond_to? :write
end

#validate_write_nonblockObject

Raises:



162
163
164
# File 'lib/brs/stream/writer.rb', line 162

def validate_write_nonblock
  raise ValidateError, "io should be responsible to write nonblock" unless @io.respond_to? :write_nonblock
end

#write(*objects) ⇒ Object

– synchronous –



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/brs/stream/writer.rb', line 25

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

IO write nonblock can raise wait writable 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 IO write nonblock. It means that first write nonblock won’t call IO write nonblock.



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/brs/stream/writer.rb', line 97

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