Class: MaxMind::DB::FileReader
- Inherits:
-
Object
- Object
- MaxMind::DB::FileReader
- Defined in:
- lib/maxmind/db/file_reader.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(filename) ⇒ FileReader
constructor
A new instance of FileReader.
- #read(offset, size) ⇒ Object
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
#size ⇒ Object (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
#close ⇒ Object
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 |