class DBus::RawMessage

A message while it is being parsed: a binary string, with a position cursor (pos), and an endianness tag.

Attributes

endianness[R]

@return [:little,:big]

pos[R]

@return [Integer] position in the byte buffer

Public Class Methods

endianness(tag_char) click to toggle source

Get the endiannes switch as a Symbol, which will make using it slightly more efficient @param tag_char [String] @return [:little,:big]

# File lib/dbus/raw_message.rb, line 36
def self.endianness(tag_char)
  case tag_char
  when LIL_END
    :little
  when BIG_END
    :big
  else
    raise InvalidPacketException, "Incorrect endianness #{tag_char.inspect}"
  end
end
new(bytes, endianness = nil) click to toggle source

@param bytes [String] @param endianness [:little,:big,nil]

if not given, read the 1st byte of *bytes*
# File lib/dbus/raw_message.rb, line 26
def initialize(bytes, endianness = nil)
  @bytes = bytes
  @pos = 0
  @endianness = endianness || self.class.endianness(@bytes[0])
end

Public Instance Methods

align(alignment) click to toggle source

Align the pos index on a multiple of alignment @param alignment [Integer] must be 1, 2, 4 or 8 @return [void]

# File lib/dbus/raw_message.rb, line 74
def align(alignment)
  case alignment
  when 1
    nil
  when 2, 4, 8
    bits = alignment - 1
    pad_size = ((@pos + bits) & ~bits) - @pos
    pad = read(pad_size)
    unless pad.bytes.all?(&:zero?)
      raise InvalidPacketException, "Alignment bytes are not NUL"
    end
  else
    raise ArgumentError, "Unsupported alignment #{alignment}"
  end
end
read(size) click to toggle source

@return [String] @raise IncompleteBufferException if there are not enough bytes remaining TODO: stress test this with encodings. always binary?

# File lib/dbus/raw_message.rb, line 56
def read(size)
  want!(size)
  ret = @bytes.slice(@pos, size)
  @pos += size
  ret
end
remaining_bytes() click to toggle source

@return [String] @api private

# File lib/dbus/raw_message.rb, line 65
def remaining_bytes
  # This returns "" if pos is just past the end of the string,
  # and nil if it is further.
  @bytes[@pos..-1]
end
want!(size) click to toggle source

@return [void] @raise IncompleteBufferException if there are not enough bytes remaining

# File lib/dbus/raw_message.rb, line 49
def want!(size)
  raise IncompleteBufferException if @pos + size > @bytes.bytesize
end