class Hurley::CompositeReadIO

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"

Constants

BINARY

Attributes

length[R]

Public Class Methods

new(length = nil, *ios) click to toggle source
# 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

Public Instance Methods

read(length = nil, outbuf = nil) click to toggle source
# 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
rewind() click to toggle source
# File lib/hurley/multipart.rb, line 218
def rewind
  @ios.each { |io| io.rewind }
  @index = 0
end

Private Instance Methods

advance_io() click to toggle source
# File lib/hurley/multipart.rb, line 229
def advance_io
  @index += 1
end
current_io() click to toggle source
# File lib/hurley/multipart.rb, line 225
def current_io
  @ios[@index]
end