Module: Pliney::IOHelpers

Included in:
IO, StringStream
Defined in:
lib/pliney/io_helpers.rb

Defined Under Namespace

Classes: StrictReadError

Instance Method Summary collapse

Instance Method Details

#read_uint16beObject Also known as: read_uint16



24
25
26
# File 'lib/pliney/io_helpers.rb', line 24

def read_uint16be
    strictread(2).unpack("n").first
end

#read_uint16leObject



41
42
43
# File 'lib/pliney/io_helpers.rb', line 41

def read_uint16le
    strictread(2).unpack("v").first
end

#read_uint32beObject Also known as: read_uint32



28
29
30
# File 'lib/pliney/io_helpers.rb', line 28

def read_uint32be
    strictread(4).unpack("N").first
end

#read_uint32leObject



45
46
47
# File 'lib/pliney/io_helpers.rb', line 45

def read_uint32le
    strictread(4).unpack("V").first
end

#read_uint64beObject Also known as: read_uint64



32
33
34
35
# File 'lib/pliney/io_helpers.rb', line 32

def read_uint64be
    v = strictread(8).unpack("NN")
    (v[0] << 32) | v[1]
end

#read_uint64leObject



49
50
51
52
# File 'lib/pliney/io_helpers.rb', line 49

def read_uint64le
    v = strictread(8).unpack("VV")
    (v[1] << 32) | v[0]
end

#read_uint8Object



20
21
22
# File 'lib/pliney/io_helpers.rb', line 20

def read_uint8
    getbyte
end

#strictread(nbytes) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pliney/io_helpers.rb', line 8

def strictread(nbytes)
    _pos = self.pos
    res = read(nbytes)
    if res.nil?
        raise(StrictReadError, "read returned nil for read(#{nbytes}) at offset #{_pos}")
    end
    if res.bytesize != nbytes
        raise(StrictReadError, "read returned only #{res.size} bytes for read(#{nbytes}) at offset #{_pos}")
    end
    return res
end