Class: HTTP::FormData::CompositeIO

Inherits:
Object
  • Object
show all
Defined in:
lib/http/form_data/composite_io.rb,
sig/http.rbs

Overview

Provides IO interface across multiple IO objects.

Instance Method Summary collapse

Constructor Details

#initialize(ios) ⇒ CompositeIO

Creates a new CompositeIO from an array of IO-like objects

Parameters:

  • ios (Array[untyped])


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_iovoid

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

Parameters:

  • length (Integer, nil) (defaults to: nil)
  • outbuf (String, nil) (defaults to: nil)

Returns:

  • (String, nil)


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

Parameters:

  • length (Integer, nil)

Yields:

Yield Parameters:

  • chunk (String)

Yield Returns:

  • (void)


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

Parameters:

  • max_length (Integer, nil)

Returns:

  • (String, nil)


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

#rewindvoid

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

#sizeInteger

Returns sum of all IO sizes

Returns:

  • (Integer)


56
57
58
# File 'lib/http/form_data/composite_io.rb', line 56

def size
  @size ||= @ios.sum(&:size)
end