]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/aviator/lib/puppet/feature/faraday/upload_io.rb
add aimonb/aviator to 3rdparty
[dsa-puppet.git] / 3rdparty / modules / aviator / lib / puppet / feature / faraday / upload_io.rb
1 begin
2   require 'composite_io'
3   require 'parts'
4   require 'stringio'
5 rescue LoadError
6   $stderr.puts "Install the multipart-post gem."
7   raise
8 end
9
10 module Faraday
11   # Similar but not compatible with ::CompositeReadIO provided by multipart-post.
12   class CompositeReadIO
13     def initialize(*parts)
14       @parts = parts.flatten
15       @ios = @parts.map { |part| part.to_io }
16       @index = 0
17     end
18
19     def length
20       @parts.inject(0) { |sum, part| sum + part.length }
21     end
22
23     def rewind
24       @ios.each { |io| io.rewind }
25       @index = 0
26     end
27
28     # Read from IOs in order until `length` bytes have been received.
29     def read(length = nil, outbuf = nil)
30       got_result = false
31       outbuf = outbuf ? outbuf.replace("") : ""
32
33       while io = current_io
34         if result = io.read(length)
35           got_result ||= !result.nil?
36           result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
37           outbuf << result
38           length -= result.length if length
39           break if length == 0
40         end
41         advance_io
42       end
43       (!got_result && length) ? nil : outbuf
44     end
45
46     def close
47       @ios.each { |io| io.close }
48     end
49
50     def ensure_open_and_readable
51       # Rubinius compatibility
52     end
53
54     private
55
56     def current_io
57       @ios[@index]
58     end
59
60     def advance_io
61       @index += 1
62     end
63   end
64
65   UploadIO = ::UploadIO
66   Parts = ::Parts
67 end