Module: Pliney::IOHelpers
- Included in:
- IO, StringStream
- Defined in:
- lib/pliney/io_helpers.rb
Defined Under Namespace
Classes: StrictReadError
Instance Method Summary collapse
- #read_uint16be ⇒ Object (also: #read_uint16)
- #read_uint16le ⇒ Object
- #read_uint32be ⇒ Object (also: #read_uint32)
- #read_uint32le ⇒ Object
- #read_uint64be ⇒ Object (also: #read_uint64)
- #read_uint64le ⇒ Object
- #read_uint8 ⇒ Object
- #strictread(nbytes) ⇒ Object
Instance Method Details
#read_uint16be ⇒ Object 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_uint16le ⇒ Object
41 42 43 |
# File 'lib/pliney/io_helpers.rb', line 41 def read_uint16le strictread(2).unpack("v").first end |
#read_uint32be ⇒ Object 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_uint32le ⇒ Object
45 46 47 |
# File 'lib/pliney/io_helpers.rb', line 45 def read_uint32le strictread(4).unpack("V").first end |
#read_uint64be ⇒ Object 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_uint64le ⇒ Object
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_uint8 ⇒ Object
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 |