class Google::APIClient::RangedIO

Wraps an input stream and limits data to a given range

@example

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

Public Class Methods

new(io, offset, length) click to toggle source

Bind an input stream to a specific range.

@param [IO] io

Source input stream

@param [Fixnum] offset

Starting offset of the range

@param [Fixnum] length

Length of range
# File lib/google/api_client/media.rb, line 54
def initialize(io, offset, length)
  @io = io
  @offset = offset
  @length = length
  self.rewind
end

Public Instance Methods

pos() click to toggle source

@see IO#pos

# File lib/google/api_client/media.rb, line 98
def pos
  @pos
end
pos=(pos) click to toggle source

@see IO#pos=

# File lib/google/api_client/media.rb, line 104
def pos=(pos)
  @pos = pos
  @io.pos = @offset + pos
end
read(amount = nil, buf = nil) click to toggle source

@see IO#read

# 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
rewind() click to toggle source

@see IO#rewind

# File lib/google/api_client/media.rb, line 92
def rewind
  self.pos = 0
end