Module: BinaryReaderMixin

Included in:
Buffer
Defined in:
lib/binary_reader.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#read_int16_bigObject



48
49
50
51
# 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_littleObject



43
44
45
46
# 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_nativeObject

Signed



39
40
41
# File 'lib/binary_reader.rb', line 39

def read_int16_native
  ru(2, 's')
end

#read_int32_bigObject



80
81
82
83
# 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_littleObject



75
76
77
78
# 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_nativeObject

Signed



71
72
73
# File 'lib/binary_reader.rb', line 71

def read_int32_native
  ru(4, 'l')
end

#read_int8Object



15
16
17
# File 'lib/binary_reader.rb', line 15

def read_int8
  ru(1, 'c')
end

#read_word16_bigObject



33
34
35
# File 'lib/binary_reader.rb', line 33

def read_word16_big
  ru(2, 'n')
end

#read_word16_littleObject



29
30
31
# File 'lib/binary_reader.rb', line 29

def read_word16_little
  ru(2, 'v')
end

#read_word16_nativeObject

Unsigned



25
26
27
# File 'lib/binary_reader.rb', line 25

def read_word16_native
  ru(2, 'S')
end

#read_word32_bigObject



65
66
67
# File 'lib/binary_reader.rb', line 65

def read_word32_big
  ru(4, 'N')
end

#read_word32_littleObject



61
62
63
# File 'lib/binary_reader.rb', line 61

def read_word32_little
  ru(4, 'V')
end

#read_word32_nativeObject

Unsigned



57
58
59
# File 'lib/binary_reader.rb', line 57

def read_word32_native
  ru(4, 'L')
end

#read_word8Object Also known as: read_byte, read_uint8

no byteorder for 8 bit!



11
12
13
# File 'lib/binary_reader.rb', line 11

def read_word8
  ru(1, 'C')
end

#readn(n) ⇒ Object

read exactly n characters, otherwise raise an exception.



101
102
103
104
105
# 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