Method: ChefAPI::Multipart::MultiIO#read

Defined in:
lib/chef-api/multipart.rb

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

Read from IOs in order until ‘length` bytes have been received.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chef-api/multipart.rb', line 49

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