Class: Hurley::CompositeReadIO

Inherits:
Object
  • Object
show all
Defined in:
lib/hurley/multipart.rb

Overview

Concatenate together multiple IO objects into a single, composite IO object for purposes of reading as a single stream.

Usage:

crio = CompositeReadIO.new(StringIO.new('one'), StringIO.new('two'), StringIO.new('three'))
puts crio.read # => "onetwothree"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(length = nil, *ios) ⇒ CompositeReadIO

Returns a new instance of CompositeReadIO.



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/hurley/multipart.rb', line 188

def initialize(length = nil, *ios)
  @ios = ios.flatten

  if length.respond_to?(:read)
    @ios.unshift(length)
  else
    @length = length || -1
  end

  @index = 0
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



186
187
188
# File 'lib/hurley/multipart.rb', line 186

def length
  @length
end

Instance Method Details

#read(length = nil, outbuf = nil) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/hurley/multipart.rb', line 200

def read(length = nil, outbuf = nil)
  got_result = false
  outbuf = outbuf ? outbuf.replace("") : ""

  while io = current_io
    if result = io.read(length)
      got_result ||= !result.nil?
      result.force_encoding(BINARY) if result.respond_to?(:force_encoding)
      outbuf << result
      length -= result.length if length
      break if length == 0
    end
    advance_io
  end

  (!got_result && length) ? nil : outbuf
end

#rewindObject



218
219
220
221
# File 'lib/hurley/multipart.rb', line 218

def rewind
  @ios.each { |io| io.rewind }
  @index = 0
end