Module: OpenC3IO
- Defined in:
- lib/openc3/core_ext/openc3_io.rb,
ext/openc3/ext/openc3_io/openc3_io.c
Overview
OpenC3 specific additions to the Ruby IO and StringIO classes
Instance Method Summary collapse
-
#read_length_bytes(*args) ⇒ String
Reads a length field and then return the String resulting from reading the number of bytes the length field indicates.
Instance Method Details
#read_length_bytes(*args) ⇒ String
Reads a length field and then return the String resulting from reading the number of bytes the length field indicates
For example:
io = StringIO.new
# where io is "\x02\x01\x02\x03\x04...."
result = io.read_length_bytes(1)
# result will be "\x01x02" because the length field was given
# to be 1 byte. We read 1 byte which is a 2. So we then read two
# bytes and return.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/openc3/core_ext/openc3_io.rb', line 42 def read_length_bytes(length_num_bytes, max_read_size = nil) return nil unless (length_num_bytes == 1) || (length_num_bytes == 2) or (length_num_bytes == 4) # Read bytes for string length temp_string_length = self.read(length_num_bytes) return nil if (temp_string_length.nil?) || (temp_string_length.length != length_num_bytes) string_length = OpenC3::BinaryAccessor.read(0, length_num_bytes * 8, :UINT, temp_string_length, :BIG_ENDIAN) # Read String return nil if max_read_size and string_length > max_read_size string = self.read(string_length) return nil if (string.nil?) || (string.length != string_length) return string end |