Class: HTTP::FormData::CompositeIO

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

Overview

Provides IO interface across multiple IO objects.

Instance Method Summary collapse

Constructor Details

#initialize(ios) ⇒ CompositeIO

Returns a new instance of CompositeIO.

Parameters:

  • ios (Array<IO>)

    Array of IO objects



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/http/form_data/composite_io.rb', line 10

def initialize(ios)
  @index  = 0
  @buffer = "".b
  @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

#read(length = nil, outbuf = nil) ⇒ String?

Reads and returns partial content acrosss multiple IO objects.

Parameters:

  • length (Integer) (defaults to: nil)

    Number of bytes to retrieve

  • outbuf (String) (defaults to: nil)

    String to be replaced with retrieved data

Returns:

  • (String, nil)


31
32
33
34
35
36
37
38
# File 'lib/http/form_data/composite_io.rb', line 31

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

#rewindObject

Rewinds all IO objects and set cursor to the first IO object.



46
47
48
49
# File 'lib/http/form_data/composite_io.rb', line 46

def rewind
  @ios.each(&:rewind)
  @index = 0
end

#sizeObject

Returns sum of all IO sizes.



41
42
43
# File 'lib/http/form_data/composite_io.rb', line 41

def size
  @size ||= @ios.map(&:size).inject(0, :+)
end