Class: Ephem::IO::BinaryReader
- Inherits:
-
Object
- Object
- Ephem::IO::BinaryReader
- Defined in:
- lib/ephem/io/binary_reader.rb
Constant Summary collapse
- RECORD_SIZE =
1024- ENDIANNESS_DOUBLE_FORMATS =
{little: "E", big: "G"}.freeze
- ENDIANNESS_UINT32_FORMATS =
{little: "V", big: "N"}.freeze
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(file_object) ⇒ BinaryReader
constructor
A new instance of BinaryReader.
- #read_array(start_word:, end_word:, endianness:) ⇒ Object
- #read_record(record_number) ⇒ Object
Constructor Details
#initialize(file_object) ⇒ BinaryReader
Returns a new instance of BinaryReader.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/ephem/io/binary_reader.rb', line 10 def initialize(file_object) validate_file_object(file_object) @file = file_object # Create a Mutex for thread-safe access to the file # Mutex ensures that only one thread can access the critical section at # a time. This prevents race conditions and data corruption when # multiple threads read from the file simultaneously @lock = Mutex.new end |
Instance Method Details
#close ⇒ Object
43 44 45 |
# File 'lib/ephem/io/binary_reader.rb', line 43 def close @file.close end |
#read_array(start_word:, end_word:, endianness:) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ephem/io/binary_reader.rb', line 30 def read_array(start_word:, end_word:, endianness:) validate_array_bounds(start_word, end_word) length = end_word - start_word + 1 # Use the Mutex to synchronize access to the file only one thread can # enter this critical section at a time. This ensures atomic reads of # the array data, preventing data corruption @lock.synchronize do seek_to_word(start_word) read_array_data(length, endianness) end end |
#read_record(record_number) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/ephem/io/binary_reader.rb', line 21 def read_record(record_number) validate_record_number(record_number) @lock.synchronize do seek_to_record(record_number) read_and_pad_record end end |