Class: Google::Apis::Core::CompositeIO

Inherits:
Object
  • Object
show all
Defined in:
lib/google/apis/core/composite_io.rb

Instance Method Summary collapse

Constructor Details

#initialize(*ios) ⇒ CompositeIO

Returns a new instance of CompositeIO



37
38
39
40
41
42
# File 'lib/google/apis/core/composite_io.rb', line 37

def initialize(*ios)
  @ios = ios.flatten
  @pos = 0
  @index = 0
  @sizes = @ios.map(&:size)
end

Instance Method Details

#posObject



69
70
71
# File 'lib/google/apis/core/composite_io.rb', line 69

def pos
  @pos
end

#pos=(pos) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/google/apis/core/composite_io.rb', line 73

def pos=(pos)
  fail ArgumentError, "Position can not be negative" if pos < 0
  @pos = pos
  new_index = nil
  @ios.each_with_index do |io,idx|
    size = io.size
    if pos <= size
      new_index ||= idx
      io.pos = pos
      pos = 0
    else
      io.pos = size
      pos -= size
    end
  end
  @index = new_index unless new_index.nil?
end

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/google/apis/core/composite_io.rb', line 44

def read(length = nil, buf = nil)
  buf = buf ? buf.replace('') : ''

  begin
    io = @ios[@index]
    break if io.nil?
    result = io.read(length)
    if result
      buf << result
      if length
        length -= result.length
        break if length == 0
      end
    end
    @index += 1
  end while @index < @ios.length
  buf.length > 0 ? buf : nil
end

#rewindObject



91
92
93
# File 'lib/google/apis/core/composite_io.rb', line 91

def rewind
  self.pos = 0
end

#sizeObject Also known as: length



63
64
65
# File 'lib/google/apis/core/composite_io.rb', line 63

def size
  @sizes.reduce(:+)
end