Class: Google::APIClient::RangedIO

Inherits:
Object
  • Object
show all
Defined in:
lib/google/api_client/media.rb

Overview

Wraps an input stream and limits data to a given range

Examples:

chunk = Google::APIClient::RangedIO.new(io, 0, 1000)

Instance Method Summary collapse

Constructor Details

#initialize(io, offset, length) ⇒ RangedIO

Bind an input stream to a specific range.

Parameters:

  • io (IO)

    Source input stream

  • offset (Fixnum)

    Starting offset of the range

  • length (Fixnum)

    Length of range



54
55
56
57
58
59
# File 'lib/google/api_client/media.rb', line 54

def initialize(io, offset, length)
  @io = io
  @offset = offset
  @length = length
  self.rewind
end

Instance Method Details

#posObject

See Also:

  • IO#pos


98
99
100
# File 'lib/google/api_client/media.rb', line 98

def pos
  @pos
end

#pos=(pos) ⇒ Object

See Also:

  • IO#pos=


104
105
106
107
# File 'lib/google/api_client/media.rb', line 104

def pos=(pos)
  @pos = pos
  @io.pos = @offset + pos
end

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

See Also:

  • IO#read


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/google/api_client/media.rb', line 63

def read(amount = nil, buf = nil)
  buffer = buf || ''
  if amount.nil?
    size = @length - @pos
    done = ''
  elsif amount == 0
    size = 0
    done = ''
  else 
    size = [@length - @pos, amount].min
    done = nil
  end

  if size > 0
    result = @io.read(size)
    result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
    buffer << result if result
    @pos = @pos + size
  end

  if buffer.length > 0
    buffer
  else
    done
  end
end

#rewindObject

See Also:

  • IO#rewind


92
93
94
# File 'lib/google/api_client/media.rb', line 92

def rewind
  self.pos = 0
end