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



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

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

Instance Method Details

#posObject

See Also:

  • IO#pos


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

def pos
  @pos
end

#pos=(pos) ⇒ Object

See Also:

  • IO#pos=


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

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

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

See Also:

  • IO#read


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
89
# File 'lib/google/api_client/media.rb', line 64

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


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

def rewind
  self.pos = 0
end