# File lib/ole/ranges_io.rb, line 197
  def write data
    return 0 if data.empty?
    data_pos = 0
    # if we don't have room, we can use the truncate hook to make more space.
    if data.length > @size - @pos
      begin
        truncate @pos + data.length
      rescue NotImplementedError
        raise IOError, "unable to grow #{inspect} to write #{data.length} bytes" 
      end
    end
    pos, len = @ranges[@active]
    diff = @pos - @offsets[@active]
    pos += diff
    len -= diff
    loop do
      @io.seek pos
      if data_pos + len > data.length
        chunk = data[data_pos..-1]
        @io.write chunk
        @pos += chunk.length
        data_pos = data.length
        break
      end
      @io.write data[data_pos, len]
      @pos += len
      data_pos += len
      break if @active == @ranges.length - 1
      @active += 1
      pos, len = @ranges[@active]
    end
    data_pos
  end