class Racknga::Middleware::Range::RangeStream

@private

Public Class Methods

new(body, first_byte, length) click to toggle source
# File lib/racknga/middleware/range.rb, line 122
def initialize(body, first_byte, length)
  @body = body
  @first_byte = first_byte
  @length = length
end

Public Instance Methods

each() { |chunk| ... } click to toggle source
# File lib/racknga/middleware/range.rb, line 128
def each
  if @body.respond_to?(:seek)
    @body.seek(@first_byte)
    start = 0
  else
    start = @first_byte
  end
  rest = @length

  @body.each do |chunk|
    if chunk.respond_to?(:encoding)
      if chunk.encoding != Encoding::ASCII_8BIT
        chunk = chunk.dup.force_encoding(Encoding::ASCII_8BIT)
      end
    end

    chunk_size = chunk.size
    if start > 0
      if chunk_size < start
        start -= chunk_size
        next
      else
        chunk = chunk[start..-1]
        chunk_size -= start
        start = 0
      end
    end
    if rest > chunk_size
      yield(chunk)
      rest -= chunk_size
    else
      yield(chunk[0, rest])
      rest -= rest
    end
    break if rest <= 0
  end
end