Module: BinaryReader

Defined in:
lib/mega/binaryreader.rb

Instance Method Summary collapse

Instance Method Details

#byte_orderObject Also known as: byteorder

default is native byte-order



43
44
45
# File 'lib/mega/binaryreader.rb', line 43

def byte_order
  @byte_order || ByteOrder::Native
end

#byte_order=(new_byteorder) ⇒ Object Also known as: byteorder=



47
48
49
# File 'lib/mega/binaryreader.rb', line 47

def byte_order=(new_byteorder)
  @byte_order = new_byteorder
end

#read_cstringObject



157
158
159
160
161
162
163
# File 'lib/mega/binaryreader.rb', line 157

def read_cstring
  str = ""
  while (c=readn(1)) != "\0"
    str << c
  end
  str
end

#read_int16_bigObject



93
94
95
96
# File 'lib/mega/binaryreader.rb', line 93

def read_int16_big
  # swap bytes if native=little (but we want big)
  ru_swap(2, 's', ByteOrder::Little) 
end

#read_int16_littleObject



88
89
90
91
# File 'lib/mega/binaryreader.rb', line 88

def read_int16_little
  # swap bytes if native=big (but we want little)
  ru_swap(2, 's', ByteOrder::Big)
end

#read_int16_nativeObject

Signed



84
85
86
# File 'lib/mega/binaryreader.rb', line 84

def read_int16_native
  ru(2, 's')
end

#read_int32_bigObject



125
126
127
128
# File 'lib/mega/binaryreader.rb', line 125

def read_int32_big
  # swap bytes if native=little (but we want big)
  ru_swap(4, 'l', ByteOrder::Little) 
end

#read_int32_littleObject



120
121
122
123
# File 'lib/mega/binaryreader.rb', line 120

def read_int32_little
  # swap bytes if native=big (but we want little)
  ru_swap(4, 'l', ByteOrder::Big) 
end

#read_int32_nativeObject

Signed



116
117
118
# File 'lib/mega/binaryreader.rb', line 116

def read_int32_native
  ru(4, 'l')
end

#read_int8Object



62
63
64
# File 'lib/mega/binaryreader.rb', line 62

def read_int8
  ru(1, 'c')
end

#read_word16_bigObject



78
79
80
# File 'lib/mega/binaryreader.rb', line 78

def read_word16_big
  ru(2, 'n')
end

#read_word16_littleObject



74
75
76
# File 'lib/mega/binaryreader.rb', line 74

def read_word16_little
  ru(2, 'v')
end

#read_word16_nativeObject

Unsigned



70
71
72
# File 'lib/mega/binaryreader.rb', line 70

def read_word16_native
  ru(2, 'S')
end

#read_word32_bigObject



110
111
112
# File 'lib/mega/binaryreader.rb', line 110

def read_word32_big
  ru(4, 'N')
end

#read_word32_littleObject



106
107
108
# File 'lib/mega/binaryreader.rb', line 106

def read_word32_little
  ru(4, 'V')
end

#read_word32_nativeObject

Unsigned



102
103
104
# File 'lib/mega/binaryreader.rb', line 102

def read_word32_native
  ru(4, 'L')
end

#read_word8Object Also known as: read_uint8

no byteorder for 8 bit!



58
59
60
# File 'lib/mega/binaryreader.rb', line 58

def read_word8
  ru(1, 'C')
end

#readn(n) ⇒ Object

read exactly n characters, otherwise raise an exception.



166
167
168
169
170
# File 'lib/mega/binaryreader.rb', line 166

def readn(n)
  str = read(n)
  raise "couldn't read #{n} characters" if str.nil? or str.size != n
  str
end