Sha256: be5ad1f524b3fde7e5f75664e2543b09ccd1f549d4c4e71568349ceb8c3329ee

Contents?: true

Size: 1.76 KB

Versions: 23

Compression:

Stored size: 1.76 KB

Contents

# encoding: ascii-8bit

# Copyright 2014 Ball Aerospace & Technologies Corp.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
# under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 3 with
# attribution addendums as found in the LICENSE.txt

require 'cosmos/packets/binary_accessor'
require 'cosmos/ext/cosmos_io' if RUBY_ENGINE == 'ruby' and !ENV['COSMOS_NO_EXT']

# COSMOS specific additions to the Ruby IO and StringIO classes
module CosmosIO
  if RUBY_ENGINE != 'ruby' or ENV['COSMOS_NO_EXT']
    # 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.
    #
    # @param length_num_bytes [Integer] Number of bytes in the length field
    # @return [String] A String of "length field" number of bytes
    def read_length_bytes(length_num_bytes)
      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 = Cosmos::BinaryAccessor.read(0, length_num_bytes * 8, :UINT, temp_string_length, :BIG_ENDIAN)

      # Read String
      string = self.read(string_length)
      return nil if (string.nil?) || (string.length != string_length)
      return string
    end
  end

end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
cosmos-4.2.4-java lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.2.4 lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.2.3-java lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.2.3 lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.2.2-java lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.2.2 lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.2.1-java lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.2.1 lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.2.0-java lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.2.0 lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.1.1-java lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.1.1 lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.1.0-java lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.1.0 lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.0.3-java lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.0.3-universal-java-1.8 lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.0.3 lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.0.2-universal-java-1.8 lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.0.2 lib/cosmos/core_ext/cosmos_io.rb
cosmos-4.0.1-universal-java-1.8 lib/cosmos/core_ext/cosmos_io.rb