class MaxMind::DB::FileReader

@!visibility private

Attributes

size[R]

Public Class Methods

new(filename) click to toggle source
# File lib/maxmind/db/file_reader.rb, line 9
def initialize(filename)
  @fh = File.new(filename, 'rb')
  @size = @fh.size
  @mutex = Mutex.new
end

Public Instance Methods

close() click to toggle source
# File lib/maxmind/db/file_reader.rb, line 17
def close
  @fh.close
end
read(offset, size) click to toggle source
# File lib/maxmind/db/file_reader.rb, line 21
def read(offset, size)
  return ''.b if size == 0

  # When we support only Ruby 2.5+, remove this and require pread.
  if @fh.respond_to?(:pread)
    buf = @fh.pread(size, offset)
  else
    @mutex.synchronize do
      @fh.seek(offset, IO::SEEK_SET)
      buf = @fh.read(size)
    end
  end

  raise InvalidDatabaseError, 'The MaxMind DB file contains bad data' if buf.nil? || buf.length != size

  buf
end