Class: Feh::Bin::ArrayIStream
- Inherits:
-
Object
- Object
- Feh::Bin::ArrayIStream
- Extended by:
- Forwardable
- Defined in:
- lib/feh/bin/array_istream.rb
Overview
Single-pass input array stream that reads little-endian integers.
Instance Attribute Summary collapse
-
#bytes_read ⇒ Integer
readonly
The number of bytes read so far.
-
#size ⇒ Integer
readonly
The size of the underlying array stream.
Instance Method Summary collapse
-
#initialize(buffer) ⇒ ArrayIStream
constructor
Initializes the stream.
-
#remaining ⇒ Array<Integer>
Returns the unread bytes of the stream.
-
#u16 ⇒ Integer?
Attempts to read an unsigned 16-bit integer.
-
#u32 ⇒ Integer?
Attempts to read an unsigned 32-bit integer.
-
#u8 ⇒ Integer?
Attempts to read an unsigned 8-bit integer.
Constructor Details
#initialize(buffer) ⇒ ArrayIStream
Initializes the stream.
20 21 22 23 24 25 26 |
# File 'lib/feh/bin/array_istream.rb', line 20 def initialize(buffer) raise ArgumentError, 'Input is not a byte array' unless buffer.is_a?(Array) && buffer.all? {|x| x.is_a?(Integer) && x.between?(0, 255)} @buf = buffer @bytes_read = 0 end |
Instance Attribute Details
#bytes_read ⇒ Integer (readonly)
Returns the number of bytes read so far.
15 16 17 |
# File 'lib/feh/bin/array_istream.rb', line 15 def bytes_read @bytes_read end |
#size ⇒ Integer (readonly)
Returns the size of the underlying array stream.
12 |
# File 'lib/feh/bin/array_istream.rb', line 12 def_delegators :@buf, :[], :size |
Instance Method Details
#remaining ⇒ Array<Integer>
Returns the unread bytes of the stream.
64 65 66 |
# File 'lib/feh/bin/array_istream.rb', line 64 def remaining @buf[@bytes_read..-1] end |
#u16 ⇒ Integer?
Attempts to read an unsigned 16-bit integer.
41 42 43 44 45 46 47 |
# File 'lib/feh/bin/array_istream.rb', line 41 def u16 return nil if @bytes_read > @buf.size - 2 x = @buf[@bytes_read] x |= @buf[@bytes_read + 1] << 8 @bytes_read += 2 x end |
#u32 ⇒ Integer?
Attempts to read an unsigned 32-bit integer.
52 53 54 55 56 57 58 59 60 |
# File 'lib/feh/bin/array_istream.rb', line 52 def u32 return nil if @bytes_read > @buf.size - 4 x = @buf[@bytes_read] x |= @buf[@bytes_read + 1] << 8 x |= @buf[@bytes_read + 2] << 16 x |= @buf[@bytes_read + 3] << 24 @bytes_read += 4 x end |
#u8 ⇒ Integer?
Attempts to read an unsigned 8-bit integer.
31 32 33 34 35 36 |
# File 'lib/feh/bin/array_istream.rb', line 31 def u8 return nil if @bytes_read > @buf.size - 1 x = @buf[@bytes_read] @bytes_read += 1 x end |