Class: Overlook::ByteReader
- Inherits:
-
Object
- Object
- Overlook::ByteReader
- Extended by:
- Forwardable
- Defined in:
- lib/overlook/byte_reader.rb
Overview
Reads bytes from a given IO object.
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
Instance Method Summary collapse
- #byte ⇒ Object
- #char ⇒ Object
- #float ⇒ Object
-
#initialize(io) ⇒ ByteReader
constructor
A new instance of ByteReader.
- #read_int64 ⇒ Object (also: #int64)
- #read_short ⇒ Object (also: #short)
- #seek(len, type = IO::SEEK_CUR) ⇒ Object
- #signed_int16 ⇒ Object
- #signed_int32 ⇒ Object
- #string(len) ⇒ Object
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
#io ⇒ Object (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
#byte ⇒ Object
38 39 40 |
# File 'lib/overlook/byte_reader.rb', line 38 def byte io.read(1).ord end |
#char ⇒ Object
42 43 44 |
# File 'lib/overlook/byte_reader.rb', line 42 def char byte.chr end |
#float ⇒ Object
34 35 36 |
# File 'lib/overlook/byte_reader.rb', line 34 def float @io.read(4).unpack('<e').first end |
#read_int64 ⇒ Object 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_short ⇒ Object 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_int16 ⇒ Object
50 51 52 |
# File 'lib/overlook/byte_reader.rb', line 50 def signed_int16 io.read(2).unpack('<s*').first end |
#signed_int32 ⇒ Object
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 |