Class: Overlook::ByteReader

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/overlook/byte_reader.rb

Overview

Reads bytes from a given IO object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ ByteReader

Returns a new instance of ByteReader.



10
11
12
# File 'lib/overlook/byte_reader.rb', line 10

def initialize(io)
  @io = io
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



8
9
10
# File 'lib/overlook/byte_reader.rb', line 8

def io
  @io
end

Instance Method Details

#byteObject



38
39
40
# File 'lib/overlook/byte_reader.rb', line 38

def byte
  io.read(1).ord
end

#charObject



42
43
44
# File 'lib/overlook/byte_reader.rb', line 42

def char
  byte.chr
end

#floatObject



34
35
36
# File 'lib/overlook/byte_reader.rb', line 34

def float
  @io.read(4).unpack('<e').first
end

#read_int64Object Also known as: int64



14
15
16
17
18
# File 'lib/overlook/byte_reader.rb', line 14

def read_int64
  io.read(8).unpack("C*").each_with_index.reduce(0) do |sum, (byte, index)|
    sum + byte * (256 ** index)
  end
end

#read_shortObject Also known as: short



21
22
23
# File 'lib/overlook/byte_reader.rb', line 21

def read_short
  io.read(2).unpack('S*').first
end

#seek(len, type = IO::SEEK_CUR) ⇒ Object



30
31
32
# File 'lib/overlook/byte_reader.rb', line 30

def seek(len, type = IO::SEEK_CUR)
  io.seek(len, type)
end

#signed_int16Object



50
51
52
# File 'lib/overlook/byte_reader.rb', line 50

def signed_int16
  io.read(2).unpack('<s*').first
end

#signed_int32Object



46
47
48
# File 'lib/overlook/byte_reader.rb', line 46

def signed_int32
  io.read(4).unpack('<l*').first
end

#string(len) ⇒ Object



26
27
28
# File 'lib/overlook/byte_reader.rb', line 26

def string(len)
  io.read(len)
end