Class: HTTP::FormData::CompositeIO
- Inherits:
-
Object
- Object
- HTTP::FormData::CompositeIO
- Defined in:
- lib/http/form_data/composite_io.rb,
sig/http.rbs
Overview
Provides IO interface across multiple IO objects.
Instance Method Summary collapse
-
#advance_io ⇒ void
Advances cursor to the next IO object.
-
#initialize(ios) ⇒ CompositeIO
constructor
Creates a new CompositeIO from an array of IO-like objects.
-
#read(length = nil, outbuf = nil) ⇒ String?
Reads and returns content across multiple IO objects.
-
#read_chunks(length) {|chunk| ... } ⇒ void
Yields chunks with total length up to
length. -
#readpartial(max_length) ⇒ String?
Reads chunk from current IO with length up to
max_length. -
#rewind ⇒ void
Rewinds all IO objects and resets cursor.
-
#size ⇒ Integer
Returns sum of all IO sizes.
Constructor Details
#initialize(ios) ⇒ CompositeIO
Creates a new CompositeIO from an array of IO-like objects
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/http/form_data/composite_io.rb', line 16 def initialize(ios) @index = 0 @ios = ios.map do |io| if io.is_a?(String) StringIO.new(io) elsif io.respond_to?(:read) io else raise ArgumentError, "#{io.inspect} is neither a String nor an IO object" end end end |
Instance Method Details
#advance_io ⇒ void
This method returns an undefined value.
Advances cursor to the next IO object
1338 |
# File 'sig/http.rbs', line 1338
def advance_io: () -> void
|
#read(length = nil, outbuf = nil) ⇒ String?
Reads and returns content across multiple IO objects
40 41 42 43 44 45 46 47 |
# File 'lib/http/form_data/composite_io.rb', line 40 def read(length = nil, outbuf = nil) data = outbuf.clear.force_encoding(Encoding::BINARY) if outbuf data ||= "".b read_chunks(length) { |chunk| data << chunk } data unless length && data.empty? end |
#read_chunks(length) {|chunk| ... } ⇒ void
This method returns an undefined value.
Yields chunks with total length up to length
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/http/form_data/composite_io.rb', line 78 def read_chunks(length) while (chunk = readpartial(length)) yield chunk.force_encoding(Encoding::BINARY) next if length.nil? remaining = length - chunk.bytesize break if remaining.zero? length = remaining end end |
#readpartial(max_length) ⇒ String?
Reads chunk from current IO with length up to max_length
95 96 97 98 99 100 101 102 103 |
# File 'lib/http/form_data/composite_io.rb', line 95 def readpartial(max_length) while (io = @ios.at(@index)) chunk = io.read(max_length) return chunk if chunk && !chunk.empty? @index += 1 end end |
#rewind ⇒ void
This method returns an undefined value.
Rewinds all IO objects and resets cursor
67 68 69 70 |
# File 'lib/http/form_data/composite_io.rb', line 67 def rewind @ios.each(&:rewind) @index = 0 end |
#size ⇒ Integer
Returns sum of all IO sizes
56 57 58 |
# File 'lib/http/form_data/composite_io.rb', line 56 def size @size ||= @ios.sum(&:size) end |