module BinaryReaderMixin

This mixin solely depends on method read(n), which must be defined in the class/module where you mixin this module.

Public Instance Methods

read_byte()
Alias for: read_word8
read_int16_big() click to toggle source
# File lib/binary_reader.rb, line 48
def read_int16_big
  # swap bytes if native=little (but we want big)
  ru_swap(2, 's', ByteOrder::Little) 
end
read_int16_little() click to toggle source
# File lib/binary_reader.rb, line 43
def read_int16_little
  # swap bytes if native=big (but we want little)
  ru_swap(2, 's', ByteOrder::Big)
end
read_int16_native() click to toggle source

Signed

# File lib/binary_reader.rb, line 39
def read_int16_native
  ru(2, 's')
end
read_int32_big() click to toggle source
# File lib/binary_reader.rb, line 80
def read_int32_big
  # swap bytes if native=little (but we want big)
  ru_swap(4, 'l', ByteOrder::Little) 
end
read_int32_little() click to toggle source
# File lib/binary_reader.rb, line 75
def read_int32_little
  # swap bytes if native=big (but we want little)
  ru_swap(4, 'l', ByteOrder::Big) 
end
read_int32_native() click to toggle source

Signed

# File lib/binary_reader.rb, line 71
def read_int32_native
  ru(4, 'l')
end
read_int8() click to toggle source
# File lib/binary_reader.rb, line 15
def read_int8
  ru(1, 'c')
end
read_uint8()

Aliases

Alias for: read_word8
read_word16_big() click to toggle source
# File lib/binary_reader.rb, line 33
def read_word16_big
  ru(2, 'n')
end
read_word16_little() click to toggle source
# File lib/binary_reader.rb, line 29
def read_word16_little
  ru(2, 'v')
end
read_word16_native() click to toggle source

Unsigned

# File lib/binary_reader.rb, line 25
def read_word16_native
  ru(2, 'S')
end
read_word32_big() click to toggle source
# File lib/binary_reader.rb, line 65
def read_word32_big
  ru(4, 'N')
end
read_word32_little() click to toggle source
# File lib/binary_reader.rb, line 61
def read_word32_little
  ru(4, 'V')
end
read_word32_native() click to toggle source

Unsigned

# File lib/binary_reader.rb, line 57
def read_word32_native
  ru(4, 'L')
end
read_word8() click to toggle source

no byteorder for 8 bit!

# File lib/binary_reader.rb, line 11
def read_word8
  ru(1, 'C')
end
Also aliased as: read_byte, read_uint8
readn(n) click to toggle source

read exactly n characters, otherwise raise an exception.

# File lib/binary_reader.rb, line 101
def readn(n)
  str = read(n)
  raise "couldn't read #{n} characters" if str.nil? or str.size != n
  str
end

Private Instance Methods

ru(size, template) click to toggle source

shortcut method for readn+unpack

# File lib/binary_reader.rb, line 110
def ru(size, template)
  readn(size).unpack(template).first
end
ru_swap(size, template, byteorder) click to toggle source

same as method ru, but swap bytes if native byteorder == byteorder

# File lib/binary_reader.rb, line 115
def ru_swap(size, template, byteorder)
  str = readn(size)
  str.reverse! if ByteOrder.byteorder == byteorder 
  str.unpack(template).first
end