class SlicedIO

SlicedIO slices a large File into a small portion.

Public Class Methods

new(io, from, to, &block) click to toggle source
# File lib/rb1drv/sliced_io.rb, line 3
def initialize(io, from, to, &block)
  @io = io
  @from = from
  @to = to
  @block = block
  rewind
end

Public Instance Methods

read(len) click to toggle source
# File lib/rb1drv/sliced_io.rb, line 20
def read(len)
  return nil if @current >= size
  len = [len, size - @current].min
  # Notify before we read
  @block.call(@current, size)
  failed_count = 0
  begin
    @io.read(len)
  rescue Errno::EIO
    @io.seek(@from + @current)
    sleep 1
    failed_count += 1
    retry unless failed_count > 5
    raise
  end
ensure
  @current += len
end
rewind() click to toggle source
# File lib/rb1drv/sliced_io.rb, line 11
def rewind
  @io.seek(@from)
  @current = 0
end
size() click to toggle source
# File lib/rb1drv/sliced_io.rb, line 16
def size
  @size ||= @to - @from + 1
end