BinaryReader

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

Methods
Classes and Modules
Module BinaryReader::ByteOrder
Public Instance methods
byte_order()

default is native byte-order

This method is also aliased as byteorder
# File lib/facets/more/binaryreader.rb, line 69
  def byte_order
    @byte_order || ByteOrder::Native
  end
byte_order=(new_byteorder)
This method is also aliased as byteorder=
# File lib/facets/more/binaryreader.rb, line 73
  def byte_order=(new_byteorder)
    @byte_order = new_byteorder
  end
byteorder()

Alias for byte_order

byteorder=(new_byteorder)

Alias for byte_order=

read_cstring()
# File lib/facets/more/binaryreader.rb, line 183
  def read_cstring
    str = ""
    while (c=readn(1)) != "\0"
      str << c
    end
    str
  end
read_int16_big()
# File lib/facets/more/binaryreader.rb, line 119
  def read_int16_big
    # swap bytes if native=little (but we want big)
    ru_swap(2, 's', ByteOrder::Little) 
  end
read_int16_little()
# File lib/facets/more/binaryreader.rb, line 114
  def read_int16_little
    # swap bytes if native=big (but we want little)
    ru_swap(2, 's', ByteOrder::Big)
  end
read_int16_native()

Signed

# File lib/facets/more/binaryreader.rb, line 110
  def read_int16_native
    ru(2, 's')
  end
read_int32_big()
# File lib/facets/more/binaryreader.rb, line 151
  def read_int32_big
    # swap bytes if native=little (but we want big)
    ru_swap(4, 'l', ByteOrder::Little) 
  end
read_int32_little()
# File lib/facets/more/binaryreader.rb, line 146
  def read_int32_little
    # swap bytes if native=big (but we want little)
    ru_swap(4, 'l', ByteOrder::Big) 
  end
read_int32_native()

Signed

# File lib/facets/more/binaryreader.rb, line 142
  def read_int32_native
    ru(4, 'l')
  end
read_int8()
# File lib/facets/more/binaryreader.rb, line 88
  def read_int8
    ru(1, 'c')
  end
read_uint8()

Alias for read_word8

read_word16_big()
# File lib/facets/more/binaryreader.rb, line 104
  def read_word16_big
    ru(2, 'n')
  end
read_word16_little()
# File lib/facets/more/binaryreader.rb, line 100
  def read_word16_little
    ru(2, 'v')
  end
read_word16_native()

Unsigned

# File lib/facets/more/binaryreader.rb, line 96
  def read_word16_native
    ru(2, 'S')
  end
read_word32_big()
# File lib/facets/more/binaryreader.rb, line 136
  def read_word32_big
    ru(4, 'N')
  end
read_word32_little()
# File lib/facets/more/binaryreader.rb, line 132
  def read_word32_little
    ru(4, 'V')
  end
read_word32_native()

Unsigned

# File lib/facets/more/binaryreader.rb, line 128
  def read_word32_native
    ru(4, 'L')
  end
read_word8()

no byteorder for 8 bit!

This method is also aliased as read_uint8
# File lib/facets/more/binaryreader.rb, line 84
  def read_word8
    ru(1, 'C')
  end
readn(n)

read exactly n characters, otherwise raise an exception.

# File lib/facets/more/binaryreader.rb, line 192
  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)

shortcut method for readn+unpack

# File lib/facets/more/binaryreader.rb, line 201
  def ru(size, template)
    readn(size).unpack(template).first
  end
ru_swap(size, template, byteorder)

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

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