class CTI::Builder::StreamBuilder

Version

$Id: StreamBuilder.rb 902 2013-04-23 05:07:04Z miyabe $

ストリームに対して結果を構築するオブジェクトです。

end

Public Class Methods

new(out, &finish) click to toggle source

結果構築オブジェクトを作成します。

out

出力先ストリーム

&finish

結果を実際に出力する前に呼び出されるブロック。引数として結果のバイト数が渡されます

例:

SingleResult.new(StreamBuilder.new($stdout) do |length|
  print "Content-Length: #{length}\r\n\r\n"
end) do |opts|
  print "Content-Type: #{opts['mime_type']}\r\n"
end

end

    # File CTI/Builder/StreamBuilder.rb
144 def initialize(out, &finish)
145   @tempFile = Tempfile.open(['cti', '.tmp'])
146   @out = out
147   @finish = finish
148   @frgs = []
149   @first = nil
150   @last = nil
151   @onMemory = 0
152   @length = 0
153   @segment = 0
154 end

Public Instance Methods

add_block() click to toggle source
    # File CTI/Builder/StreamBuilder.rb
156 def add_block
157   id = @frgs.size
158   frg = Fragment.new(id)
159   @frgs[id] = frg
160   unless @first
161     @first = frg
162   else
163     @last.nxt = frg
164     frg.prev = @last
165   end
166   @last = frg
167 end
close_block(id) click to toggle source
    # File CTI/Builder/StreamBuilder.rb
187 def close_block(id)
188 end
dispose() click to toggle source
    # File CTI/Builder/StreamBuilder.rb
207 def dispose
208 end
finish() click to toggle source
    # File CTI/Builder/StreamBuilder.rb
194 def finish
195   begin
196     @finish.call(@length) if @finish
197     frg = @first
198     while frg
199       frg.flush(@tempFile, @out)
200       frg = frg.nxt
201     end
202   ensure
203     @tempFile.close
204   end
205 end
insert_block_before(anchor_id) click to toggle source
    # File CTI/Builder/StreamBuilder.rb
169 def insert_block_before(anchor_id)
170   id = @frgs.size
171   anchor = @frgs[anchor_id]
172   frg = Fragment.new(id)
173   @frgs[id] = frg
174   frg.prev = anchor.prev
175   frg.nxt = anchor
176   anchor.prev.nxt = frg
177   anchor.prev = frg
178   @first = frg if @first.id == anchor.id
179 end
serial_write(data) click to toggle source
    # File CTI/Builder/StreamBuilder.rb
190 def serial_write(data)
191   @out.write(data)
192 end
write(id, data) click to toggle source
    # File CTI/Builder/StreamBuilder.rb
181 def write(id, data)
182   frg = @frgs[id]
183   @onMemory, @segment, len = frg.write(@tempFile, @onMemory, @segment, data)
184   @length += len
185 end