BinReadable

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 BinReadable::ByteOrder
Public Instance methods
byte_order()

default is native byte-order

This method is also aliased as byteorder
# File lib/more/facets/binreadable.rb, line 74
  def byte_order
    @byte_order || ByteOrder::Native
  end
byte_order=(new_byteorder)
This method is also aliased as byteorder=
# File lib/more/facets/binreadable.rb, line 78
  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/more/facets/binreadable.rb, line 188
  def read_cstring
    str = ""
    while (c=readn(1)) != "\0"
      str << c
    end
    str
  end
read_int16_big()
# File lib/more/facets/binreadable.rb, line 124
  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/more/facets/binreadable.rb, line 119
  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/more/facets/binreadable.rb, line 115
  def read_int16_native
    ru(2, 's')
  end
read_int32_big()
# File lib/more/facets/binreadable.rb, line 156
  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/more/facets/binreadable.rb, line 151
  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/more/facets/binreadable.rb, line 147
  def read_int32_native
    ru(4, 'l')
  end
read_int8()
# File lib/more/facets/binreadable.rb, line 93
  def read_int8
    ru(1, 'c')
  end
read_uint8()

Alias for read_word8

read_word16_big()
# File lib/more/facets/binreadable.rb, line 109
  def read_word16_big
    ru(2, 'n')
  end
read_word16_little()
# File lib/more/facets/binreadable.rb, line 105
  def read_word16_little
    ru(2, 'v')
  end
read_word16_native()

Unsigned

# File lib/more/facets/binreadable.rb, line 101
  def read_word16_native
    ru(2, 'S')
  end
read_word32_big()
# File lib/more/facets/binreadable.rb, line 141
  def read_word32_big
    ru(4, 'N')
  end
read_word32_little()
# File lib/more/facets/binreadable.rb, line 137
  def read_word32_little
    ru(4, 'V')
  end
read_word32_native()

Unsigned

# File lib/more/facets/binreadable.rb, line 133
  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/more/facets/binreadable.rb, line 89
  def read_word8
    ru(1, 'C')
  end
readn(n)

read exactly n characters, otherwise raise an exception.

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