Class: MaxMind::DB::FileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/maxmind/db/file_reader.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ FileReader

Returns a new instance of FileReader.



8
9
10
11
12
# File 'lib/maxmind/db/file_reader.rb', line 8

def initialize(filename)
  @fh = File.new(filename, 'rb')
  @size = @fh.size
  @mutex = Mutex.new
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



14
15
16
# File 'lib/maxmind/db/file_reader.rb', line 14

def size
  @size
end

Instance Method Details

#closeObject



16
17
18
# File 'lib/maxmind/db/file_reader.rb', line 16

def close
  @fh.close
end

#read(offset, size) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/maxmind/db/file_reader.rb', line 20

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